Query command
The main query commands for DDS include:
find(): the most basic query command. The basic syntax is: db.collection.find(query, projection).
query: selection condition, similar to WHERE clause.
Projection: optional, use projection to display the queried fields, similar to SQL SELECT. The syntax { field: 1 } allows you to specify the field to be returned, where 1 represents the field to be returned and 0 represents the field not to be returned.
Example:
//Query the data with age=20
db.students.find({age: 20})
//Query the data with age=20, the query result only returns name, not _id
db.students.find({age: 20}, {name: 1, _id: 0})findOne(): finds a piece of data, and returns the first matching data. Syntax:
db.collection.findOne(query, projection)
Example:
db.students.findOne({age: 20})limit() and skip(): paging query, limit() specifies the number of data entries, skip() skips the first few entries:
db.students.find().limit(5) db.students.find().limit(5).skip(10) // Skip the first 10 entries and return 5 pieces of data
sort(): Sorting. Sort by field, with the order parameter specifying the sorting method. -1 is in descending order and 1 is in ascending order. The default ascending order is:
db.students.find().sort({age:1})
db.students.find().sort({age: -1}) //Descending ordercount(): Number of statistical results:
db.collection.find(query).count()
That is to say, the DDS query is mainly implemented through the combination of find() and query conditions, and the methods such as limit(), skip(), sort(), and count() are used in find() for query control.
Another thing to note is that the query result returns a Cursor. Cursor should be closed in time after use, otherwise memory accumulation will occur.
Write/Update command
The DDS write and update commands mainly include the following:
insert(): Insert new data.
Basic syntax:
db.collection.insert(document)
or
db.collection.insertOne(document)
Example:
db.users.insert({name:"John", age:30})
db.users.insertOne({name:"Mary", age:25})save(): Update if there is a _id, otherwise insert a new document.
db.collection.save(document)
update(): Update an existing document.
Basic syntax:
db.collection.update(query, update, {multi:true})Parameter:
query: The updated query condition.
update:{$set: {xxx: yyy}}, use the set operator to update the field.
{multi:true}: Whether to update multiple records, the default is false and only one is updated.
Example:
db.users.update({name:"John"}, {$set: {age: 31}})
db.users.update({name:"John"},{$set: {age: 31}},{multi:true})insertMany(): Insert multiple pieces of data.
db.collection.insertMany([documents])
remove(): Delete the data.
db.collection.remove(query, justOne)
Example:
db.users.remove({age: {$lt: 30}}) // Delete all documents with age less than 30
db.users.remove({name:"John"}, true) //Delete a document with the name JohnDelete command
There are several ways to delete the DDS, including:
db.collection.remove(): Delete one or more documents from the collection.
db.COLLECTION_NAME.remove({condition})Delete a document with the specified _id:
db.users.remove({_id: ObjectId("507c35dd8fada716c89d0013")})Delete multiple documents that meet the conditions:
db.users.remove({age: {$lt: 18}})This command deletes all documents with age less than 18.
db.collection.deleteOne(): Delete a document from the collection.
db.COLLECTION_NAME.deleteOne({condition})db.collection.deleteMany(): Delete multiple documents from the collection.
db.COLLECTION_NAME.deleteMany({condition})db.collection.drop(): Delete the entire collection.
db.COLLECTION_NAME.drop()
db.dropDatabase(): Delete the entire current database.
db.dropDatabase()
In general, you can use remove() or deleteOne() to delete one or a small number of documents, deleteMany() to delete multiple documents, and drop() and dropDatabase() to delete the entire collection or database.