MongoDB general commands

MongoDB general command to


create database
use DATABASE_NAME

example:
use runoob
show dbs //View all databases



delete database
db.dropDatabase()


insert document
db.COLLECTION_NAME.insert(document)

example:
db.runoob.insert({"name":"Rookie Tutorial"})



Update document
update() method
db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)

query : query condition of update, similar to sql update After the where in the query.
update : The object of update and some update operators (such as $, $inc...), etc., can also be understood as
upsert after set in the sql update query: optional, this parameter means that if there is no update Record, whether to insert objNew, true is inserted, the default is false, not inserted.
multi : optional, mongodb defaults to false, only the first record found is updated, if this parameter is true, all the records found by the condition are updated.
writeConcern : optional, the level of exception thrown.

example:
db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}})
db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}},{multi:true})


save() method
db.collection.save(
   <document>,
   {
     writeConcern: <document>
   }
)

document : document data.
writeConcern : optional, the level of exception thrown.

example:
db.col.save({
	"_id" : ObjectId("56064f89ade2f21f36b03136"),
    "title" : "MongoDB",
    "description" : "MongoDB is a Nosql database",
    "by" : "Runoob",
    "url" : "http://www.runoob.com",
    "tags" : [
            "mongodb",
            "NoSQL"
    ],
    "likes" : 110
})



remove document
db.collection.remove(
   <query>,
   <justOne>
)

query : (optional) condition for the removed document.
justOne : (optional) If set to true or 1, only one document will be deleted.

example:
db.col.remove({'title':'MongoDB Tutorial'})



Query document
db.COLLECTION_NAME.find()

db.users.find({}, {'name' : 1, 'skills' : 1});
//The first {} put the where condition and the second {} specify those Column display and non-display (0 means not display 1 means display)

example:
equal
{<key>:<value>}
db.col.find({"by":"Rookie Tutorial"}).pretty()
where by = 'Rookie Tutorial'

less than
{<key>:{$lt:<value>}}
db.col.find({"likes":{$lt:50}}).pretty()
where likes < 50

less than or equal to
{<key>:{$lte:<value>}}
db.col.find({"likes":{$lte:50}}).pretty()
where likes <= 50

more than the
{<key>:{$gt:<value>}}
db.col.find({"likes":{$gt:50}}).pretty()
where likes > 50

greater than or equal to
{<key>:{$gte:<value>}}
db.col.find({"likes":{$gte:50}}).pretty()
where likes >= 50

not equal to
{<key>:{$ne:<value>}}
db.col.find({"likes":{$ne:50}}).pretty()
where likes != 50

AND condition
db.col.find({key1:value1, key2:value2}).pretty()

OR condition
db.col.find({$or: [{key1: value1}, {key2:value2}]}).pretty()

Combining AND and OR
db.col.find({"likes": {$gt:50}, $or: [{"by": "菜鸟教程"},{"title": "MongoDB 教程"}]}).pretty()

in, not in ($in, $nin)
db.users.find({'age' : {'$in' : [10, 22, 26]}});

like (mongoDB supports regular expressions)
db.users.find({name:/hurry/}); // like "%hurry%";
db.users.find({name:/^hurry/}); //like "hurry%";

distinct deduplication
db,collection.distinct(field,query)
db.users.distinct('name');
db.collection.distinct("user",{"age":{$gt:28}});//Used to query different usernames whose age is greater than 28 years old

count simply counts the number of documents in a collection that meet a certain condition
db.collection.count(<query>)或者db.collection.find(<query>).count()

group grouping
1. If the number of grouped records is greater than 20,000, you may need other methods for statistics, such as aggregation pipeline or MapReduce;
db.runCommand({"group":{
 "ns":"vegetableprice", //Specify the name of the collection to be operated on
 "key":{"name":true}, //Specify the key to be used for grouping
 "initial":{"time":0},	//初始化,time=0
 "$reduce":function(doc, prev){ //After grouping, each grouping will be aggregated through this function
 if(doc.time > prev.time){
 prev.time = doc.time;
 prev.price = doc.price;
 }},
 "condition":{"name":{"$in":["tomato", "cucumber"]}} //Condition. The key "condition" can also be abbreviated as "cond" or "q"
 }});

$mod modulo operation
db.things.find( { a : { $mod : [ 10 , 1 ] } } );//a % 10 == 1

$size matches the number of elements in the array
db.things.find( { a : { $size: 1 } } );//{a:["foo"]},,1 is the number of matches


Conditional operator
(>) greater than - $gt
(<) less than - $lt
(>=) greater than or equal to - $gte
(<= ) less than or equal to - $lte


$type operator
db.col.find({"title" : {$type : 2}}) //Get the data whose title is String in the "col" collection

($type operator number): http://www.runoob.com/mongodb/mongodb-operators-type.html


Limit( ) method (specify the number of records read from MongoDB)
db.COLLECTION_NAME.find().limit(NUMBER)


Skip() method (skip the specified amount of data)
db.COLLECTION_NAME.find().limit(NUMBER) .skip(NUMBER)


sort
sort() method

Example :
db.COLLECTION_NAME.find().sort({KEY:1}) //1 is ascending order, -1 is descending order



Index
ensureIndex() method
db.COLLECTION_NAME.ensureIndex({KEY:1})//1 is ascending order, -1 is descending order

db.values.ensureIndex({open: 1, close: 1}, {background: true})
( index other parameters): http://www.runoob.com/mongodb/mongodb-operators-type.html


Reference: http://www.runoob.com/mongodb/mongodb-aggregate.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326933060&siteId=291194637