I. Data storage structure
MongoDB storage data is divided into 3 layers.
Dbs--collections--documents.
At the bottom is documents, which stores data in JSON format. Collections is a dynamic collection of files that can be understood as a drawer for storing files. DBS is a database that can be understood as a bookcase composed of multiple drawers.
Second, CRUD (creat, read, Updata, delete) operations
1. Create
The Db.collection.insert () method is generally used.
The following is a similar picture is official online, do very intuitive.
users is a collection of data to insert, and is created automatically if not defined. Can view collections with show collections. Show collections is a shell helper function, equivalent to Db.getcollectionnames ().
If you do not define the "_id" key after inserting it, it will be created automatically.
2. Read
Generally use the Db.collection.find () method or Db.collection.findOne ().
Query criteria is a search condition.
"$lt", "$lte", "$gt", "$gte" correspond to < respectively, <=, >=.
The or query has two methods, $in, $or. When a key needs to match multiple values, use the former, and use the latter when matching multiple keys.
Db.users.find ({users_id:{$in: [12345, "Joe"]}})
Db.restaurants.find ( "cuisine": "Italian"}, {"Address.zipcode": "10075" }]})
And $not, $and, querying arrays, etc.
Projection is the projection, which is the meaning of the return setting. If set to 1 then the specified return, set to 0 is rejected and not returned. "_ID" is returned by default.
Cursor modifier has three methods, limit (), skip (), sort (). The limits, the ignore, the sorting methods, respectively.
3. Update
The db.collection.update () method is generally used.
Update criteria is a condition for updating.
Update action is a method of updating behavior, $set, $inc, $push, and so on.
Update option is an upgrade.
4. Delete
The Db.collection.remove () method is generally used.
Using the drop () method is faster if you want to delete the entire collection.
MongoDB Preliminary Shell usage notes