A Preliminary Understanding of Mongodb Grammar (I)


MongoDB Start Server

  • Step 1: Download the installation package
  • Step 2: Unzip the downloaded installation package and find all. exe files under the bin directory
  • Step 3: Create a new “data” folder under this directory, which will serve as the root folder for data storage.
  • Step 4: Open the CMD window, switch to the directory, and enter the command as follows:mongod --dbpath G:\MongoDB\data
  • Step 4: Test whether or not to start, http://localhost:27017/

MongoDB Insert Documents

  • db.collection_name.insert(document)Useinsert()orsave()Method inserts a document into a collection. If the collection is not in the database, MongoDB automatically creates the collection and inserts the document.
  • MongoDB can define data as a variable and perform insertion operations:db.col.insert(document)If you do not specify the save () method for the _id field, it is similar to the insert () method. If the _id field is specified, the data for that _id is updated.

MongoDB grammar

  • use database_nameCommand: If the database does not exist, create the database, otherwise switch to the specified database (default is test)
  • db.dropDatabase()Command: Delete the current database by default to test
  • show dbsCommand: View all databases
  • dbCommand: View the current database name.
  • db.createCollection(name, options)Command: Create a collection

    • There is no need to create collections in MongoDB. MongoDB automatically creates collections when some documents are inserted.db.yiibai.insert({"name" : "yiibai"})
  • show collectionsCommand: Check the collection created
  • db.collection_name.drop()Command: Delete the collection from the database.
  • db.collection_name.find()Command: Query MongoDB data from a collection

    • find()All files will be displayed in an unstructured manner. If the display results are formatted, you can usedb.collection_name.find().pretty()
      exceptfind()There are also waysfindOne()Method that returns only one document.
    • MongoDB projection selects only the data needed, not the data of the entire document.db.collection_name.find({},{KEY:1})
  • db.mycol.update({key1:value1},{$set:{key2:value2}})Command: Update values in existing documents

    • By default, MongoDB will only update a single file, with more updates, requiring a parameter 'multi' Set totruedb.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}},{multi:true})
  • db.mycol.remove(DELLETION_CRITTERIA)Command: Used to delete documents from collections

    • db.mycol.remove()Command: Delete all files
  • RDBMS Where clause is equivalent to MongoDB

    • AND Conditions: Usedb.mycol.find({key1:value1, key2:value2}).pretty()command
    • OR condition: Usedb.mycol.find({$or: [{key1: value1}, {key2:value2}]}).pretty()You need to use the $or keyword
    • Use the AND and OR conditions: Usedb.mycol.find({"likes": {$gt:10}, $or: [{key1:value1}, { key2:value2}] }).pretty()command
  • Record: Limit () method,db.collection_name.find().limit(NUMBER),skip()It also accepts numeric type parameters and is used to skip the number of files.

    • db.collection_name.find().limit(NUMBER).skip(NUMBER)
  • Document sorting: sort () method, sort () method accepts a document containing a list of fields and sorting order. Use 1 and – 1 to specify the sort order. 1 is used for ascending, while – 1 is used for descending.

    • db.mycol.find({},{"title":1,_id:0}).sort({"title":-1})
  • MongoDB Index: Usedb.mycol.ensureIndex({"title":1,"description":-1})

    • Indexes support efficient query execution. Indexes are special data structures that store a small portion of data in a traversal form that is easy to set. The index stores the value of a specified field or a set of fields in the index, and sorts the values of the fields.
  • MongoDB Aggregation: Usingdb.yibai.aggregate([{$group:{_id:"$name",num:{$sum:1}}}])

    • The above uses will be equivalent to SQL queries:select name, count(*) from mycol group by name
  • MongoDB replication: By using replication of replica sets. A replica set is a set of mongod instances that carry the same data set. One node in the replica is the primary node that receives all write operations. All instances, secondary, application operations are subordinate so that they have the same data set. The replica set can only have one primary node.
  • Aggregate expression

    • $sumAccumulate the defined values in all documents from the collection
    • $avgCalculate the average of all given values from all documents in the collection
    • $minThe minimum corresponding value of all files retrieved from the collection
    • $maxGet the maximum from the corresponding values in all documents in the collection
    • $pushInsert array values into documents
    • $addToSetArrays generated by inserting values into documents without duplication