mongoDB usage notes

mongoDB is a document database, which supports various operations of addition, deletion, modification and query. The command form is more similar to OO, and the statement form is "database.collection.operation({argc})". As a document database, mongoDB does not need to initialize the database and document set in advance. It will be initialized automatically in the first operation.


insertOne and insertMany are supported. The former directly inserts a document in JSON format as argc, and the latter inserts a document array [doc1,doc2...], Note that there can be only one element in the array.

  name: "Joe",
  gender: "m"

  name: "Jason",
  birthdate: { "day": 3, "month": 3, "year": 1996 },
  hobby: ["basketball", "running", "traveling"]
  name: "Jason",
  birthdate: { "day": 3, "month": 3, "year": 1996 },
  hobby: ["basketball", "running", "traveling"]


It also supports two operations findOne and find, which return all qualified documents

find contains two variables, the first limits the search range and the second limits the return value; If only one variable is entered, all the contents of the matching documents will be returned; if no variable is entered, all the contents of all documents will be returned

Exact search

The first variable is also in the format of JSON document. If an ordinary JSON document is input, the document matching the corresponding variable will be returned (without limiting the key values not mentioned)

// Returns all documents with gender attribute f
db.student.find({ gender: "f" })
// If it is a multi-level document, use the similar writing method and enclose it in quotation marks. A good habit is to keep the key values enclosed in quotation marks
db.student.find({ "birthdate.year": 1998 })
// The writing method of array is similar. The following returns the document with the first element of time as 9
// If it is written in the form of multi-level document, the fully compliant document will be displayed. The following statement will not return the document containing other attributes in birthdate and year=1998
db.student.find({ birthdate: {year: 1998 }})

Condition search

mongoDB supports a variety of conditional search methods. Its reserved words are uniformly identified by $. The specific commands are similar to the latex syntax. Use braces to indicate the level between conditions.

Limited scope

For numbers, $lt,$gt,$gte,$lte can be used to limit the upper and lower bounds

// Return documents with age less than 24
db.student.find({age : { $lt: 24 } })
// Returns documents with elements less than 9 and elements greater than 18 in the array
db.student.find( { time: { $lt: 9, $gt: 18} } )

For ordinary attributes, $in and $all can be used to indicate the range. The former is the array in which the attribute is listed, and the latter is the array in which the attribute array contains the listed array

// Returns a document whose city is one of the following
db.student.find({ city: { $in: ["Shanghai", "Hangzhou", "Chengdu"] } })
// The returned hobby contains the following documents
db.student.find( { hobby: { $all: [ "photography", "cooking" ] } } )

Logical expression, $or represents a document that meets one of the following conditions

// Returns the document that satisfies any element in the array
db.student.find({ $or: [ { city: "Shanghai" }, { age: { $lt: 23 } } ] })

Regular expressions, circled with / /

// name starts with J
db.student.find( {name: /^J/ } )

Matching of arrays, simple {cond1,cond2...} If there are elements in the whole array that meet all conditions, use the $elemMatch statement if you want an element to meet all conditions

// Returns a document in the array that contains elements that satisfy both age and name
db.tutor.find( {"students": { $elemMatch: { age: { $lt: 22 }, name: /^J/ } } } )
// Returns the documents that satisfy the age element and the name element in the array
db.tutor.find( { "students.age": { $lt:22 },  "":  /^J/} )

Output limit

For the second parameter, the form of key:0 or key:1 is used to indicate whether to output. When there is a key:1 statement, the attributes not mentioned are not output (_id excluded), otherwise all the attributes not marked as key:0 are output

// Return_ id, and name
db.student.find( { gender: "m" }, { name: 1 } ) (Default include_id)
// Returns all attributes except birthdate and hobby
db.student.find( { gender: "m" }, { birthdate: 0, hobby: 0 } )


Both updateOne and updateMany operations are supported. Two parameters are required. The first parameter is completely consistent with the first parameter of find to limit the scope of the operation, and the second parameter indicates the modification method


$set sets the mentioned attribute to the corresponding value. You can wrap $set without quotation marks

\\ All male city Set as Shanghai
db.student.updateMany (
  {gender: "m"},
  {"$set": {city: "Shanghai"}}


$inc adds the corresponding value to the mentioned attribute (which can be negative). If the modified value or attribute itself is a string, an error will be reported

\\ All documents with birth month less than 9 age++
db.student.updateMany (
  { "birthdate.month": { $lt: 9 } },
  { "$inc": {age: 1} }


It also supports deleteOne and deleteMany. The syntax of the parameter is the same as that of the first parameter of find

Posted by solarith on Sun, 15 May 2022 16:50:22 +0300