In MongoDB, a database might contain multiple collections, just as a database in MySQL might contain multiple tables, and a collection might contain multiple documents, just as a table in MySQL contains more than one piece of data.
Basic Operations Command:show dbs View all database lists use <dbname> connection to the specified database db view the currently used database > use testdb # Create a TestDB database switched to db testdb> show dbsadmin 0.078gbceilometer 1.953gblocal &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;6.075GB listed in the database does not have TestDB or display TestDB (empty), because there is nothing in TestDB. > use testdb # Delete TestDB Database switched to db testdb> Db.dropdatabase () { "Dropped" : "TestDB", "OK" : 1 }> use testdb # Create Collection switched to db testdb> db.createcollection (' users ') { "OK" : 1 }> show collectionssystem.indexesusers> show collections # Delete Collection System.indexesusers> db.users.drop () true> show collectionssystem.indexes> Db.createcollection (' users ') # insert data into the collection if the collection is not automatically created { "OK" : 1 }> show collectionssystem.indexesusers> db.users.insert ([... { name : ' Yao ',... email : ' [email protected] ' ...  ,},... { name: ' shen ',... email : ' [email protected] ' ... }... ]) Bulkwriteresult ({"Writeerrors" : [ ], "writeconcernerrors" &NBSP;:&NBSP;[&NBSP;], "ninserted" : 2, "nupserted" : 0, " Nmatched " : 0," nmodified " : 0," NremoVed " : 0," upserted "&NBSP;:&NBSP;[&NBSP;]}) > db.users.save ([ # save can also implement the above Insert function ... { name : ' test ',... email : ' [email protected] ' ... }... ]) Bulkwriteresult ({"writeerrors" &NBSP;:&NBSP;[&NBSP;], " Writeconcernerrors "&NBSP;:&NBSP;[&NBSP;]," ninserted " : 1," nupserted " : 0," nMatched "&NBSP;: 0, "nmodified" : 0, "nremoved" : 0, "upserted" &NBSP;:&NBSP;[&NBSP;]}) data query command:> Db.users.find () # find returns all data records without any parameters { "_id" : objectid ("55e1cf2326f975bfd9b902e9"), "name" &NBSP;: "Yao", "email" : "[email protected]" }{ "_id" : objectid (" 55e1cf2326f975bfd9b902ea "), " name "&NBsp;: "shen", "email" : "[email protected]" }{ "_id" : objectid (" 55e1d32b26f975bfd9b902ec "), " name " : " test ", " email " : " [email protected] " }> db.users.find (). Pretty () # pretty query output more beautiful display {"_id" &NBSP;: objectid ("55e1cf2326f975bfd9b902e9"), "name" : "Yao", "email" : "[email protected ] "} {" _id " : objectid (" 55e1cf2326f975bfd9b902ea ")," name " : " shen "," email " : " [ Email protected] "} {" _id " : objectid (" 55e1d32b26f975bfd9b902ec ")," name " : " Test "," Email " : " [email protected] "}> db.users.find ({' name ': ' Yao ', ' email ': ' [email Protected]). Pretty () # find pass in multiple key-value pairs, MongoDB for multiple key-value pairs as and processing {"_id" : objectid (" 55e1cf2326f975bfd9b902e9 ")," name " : " Yao "," email " : " [email protected] "}> Db.users.find ({$or: [{' name ': ' YAo '}, {' email ': ' [email protected] '} '}). Pretty () # or query statement requires $or keyword {"_id" : ObjectId ("55e1cf2326f975bfd9b902e9"), "name" : "Yao", "email" : "[email protected]"} {"_ ID " : objectid" ("55e1d32b26f975bfd9b902ec"), "name" : "test", "email" : "[email protected] "}> db.users.find ({' name ': ' Yao ', $or: [{' name ': ' Yao '}, {' email ': ' [ Email protected]}). Pretty () # and, or together with {"_id" : objectid ("55e1cf2326f975bfd9b902e9 ")," name " : " Yao "," email " : " [email protected] "}
This article is from the "The-way-to-cloud" blog, make sure to keep this source http://iceyao.blog.51cto.com/9426658/1689662
MongoDB Basic Operations