MongoDB journey (ii) basic operations (MongoDBJavascriptShell) after learning about MongoDB, I will briefly introduce some simple operations on MongoDB. MongoDBjavascriptshell is a javascript-based interpreter that supports js programs. For (i0; i200000; I ++) {db. numbers. save ({num: I
MongoDB journey (ii) basic operations (MongoDB Javascript Shell) after learning about MongoDB, here is a brief introduction to some simple operations on MongoDB. MongoDB javascript shell is a javascript-based interpreter, so it supports js programs. For (I = 0; i200000; I ++) {db. numbers. save ({num: I
MongoDB journey (2) basic operations (MongoDB Javascript Shell)
After learning about MongoDB, I will briefly introduce some simple operations on MongoDB.
MongoDB javascript shell is a javascript-based interpreter, so it supports js programs.
As follows:
For (I = 0; I <200000; I ++ ){
Db. numbers. save ({num: I });
}
------------------- The following describes some simple operations -----------------
Use tutorial;
Db. users. insert ({username: "smith"}); db. users. save ({username: "jone "});
Db. users. find (); db. uers. find ({username: "smith "});
Db. users. count ();
Db. users. update ({username: "smith" },{$ set: {country: "Canada "}});
Db. users. update ({username: "smith" },{$ unset: {country: 1 }});
Db. users. update ({username: "smith"}, {$ set: {favorites: {city: ["Beijing", "Chicago"], movies: ["American Beauty ", "..... "] }});
Db. users. find ({"favorites. movies": "American Beauty "});
Adding a new element to favorites. movies can be used as follows: $ push, $ addToSet, which guarantees uniqueness.
Db. users. find ({username: "smith"}, {$ addToSet: {"favorites. movies": "The Maltess Falcon" }}, false, true );
Parameter 1: Query condition; parameter 2: add element; parameter 4: multi-update ); by default, MongoDB update operations are only applied to the first document matched by the query selector. If you want the operation to be applied to all matched documents, you need to display the description.
Db. foo. remove (); delete the data in the document. The foo file itself will not be deleted, for example, the delete/truncate tablem command;
Db. users. drop (); Delete the document.
Others:
· Db. members. find ({num: {"$ gt": 1999995 }});
Db. members. find ({num: {"$ gt": 20, "$ lt": 25 }});
· Db. members. find ({num: {"$ gt": 1999995 }}). explain (); [BasicCursor: No index is used; n: number of results; nscanned: Number of scans]
· The ascending index of num is as follows: db. members. ensureIndex ({num: 1}); View index creation: db. members. getIndexes ();