The db.collection.find() method returns a cursor. To access the documents, you need to iterate the cursor. However, in the mongo shell, if the returned cursor is not assigned to a variable using the varkeyword, then the cursor is automatically iterat…
By default, queries in MongoDB return all fields in matching documents. To limit the amount of data that MongoDB sends to applications, you can include a projection document in the query operation. Projection Document The projection document limits t…
Query Method MongoDB provides the db.collection.find() method to read documents from a collection. The db.collection.find() method returns a cursor to the matching documents. db.collection.find( <query filter>, <projection> ) For the db.collec…
Different query operators in MongoDB treat null values differently. The examples on this page use the db.collection.find() method in the mongo shell. To populate the users collection referenced in the examples, run the following in mongo shell: db.us…
1 迭代游标 } ); while (myCursor.hasNext()) { print(tojson(myCursor.next())); } } ); myCursor.forEach(printjson); 2 迭代指数 } ); var documentArray = myCursor.toArray(); ]; 可以简化成 } ); ]; 3 关闭游标,不活跃的游标需要等到10分钟才自动关闭,可以通过指定时间来进行关闭 var myCursor = db.users.find().…
Delete Methods MongoDB provides the following methods to delete documents of a collection: Method Description  db.collection.remove()  Delete a single document or all documents that match a specified filter.  db.collection.deleteOne() Delete at most…
MongoDB provides the following methods for inserting documents into a collection: db.collection.insertOne() db.collection.insertMany() db.collection.insert() This page provides examples of insert operations in the mongo shell. Insert Behavior Collect…
Update Methods MongoDB provides the following methods for updating documents in a collection: Method Description  db.collection.updateOne() Updates at most a single document that match a specified filter even though multiple documents may match the s…
mongo Shell Command History You can retrieve previous commands issued in the mongo shell with the up and down arrow keys. Command history is stored in ~/.dbshell file. See .dbshell for more information. Command Line Options The mongo shell can be sta…
Introduction The mongo shell is an interactive JavaScript interface to MongoDB. You can use the mongo shell to query and update data as well as perform administrative operations. The mongo shell is a component of the MongoDB distributions. Once you h…