MongoDB simple operation

With the MongoDB for some time, has been used in the project, today summarizes the various operations under the database

Inquire

According to conditions of inquiry

db.getCollection('Message').find({"ServerId" : 3326})

 

Multi-criteria query

db.getCollection('MuteAudit').find({"GameId":2109,"Unit" : 1000,"RoleId" : 723129})

 

Find data item ID is greater than 2 $ gt - greater than>

db.getCollection('Goods').find({GoodsId:{'$gt':2}})


Id equal to the data items to find 2 $ gte - gt equal> =

db.getCollection('Goods').find({GoodsId:{'$gte':2}})

 

Queries Item ID is less than 6 $ lt - less than <

db.getCollection('Goods').find({GoodsId:{'$lte':6}})

 

Item ID query data less the $ lte 6 - lt equal <=

db.getCollection('Goods').find({GoodsId:{'$lte':6}})

 

Queries ID that contains the data items of $ ne 6 -! Not equal =

db.getCollection('Goods').find({GoodsId:{'$ne':6}})

 

Query data item ID is equal to 6 $ eq - equal =

db.getCollection('Goods').find({GoodsId:{'$eq':6}})

 

Data query specified range

db.getCollection('Goods').find({GoodsId:{'$gte':1,'$lte':6}})

 

Search record certain period of time

db.getCollection('Message').find({"CreateTime" : { "$gte" : ISODate("2017-08-20T00:00:00Z")
, "$lt" : ISODate("2018-08-21T00:00:00Z")}})

 

A query string contains certain characters use regex

db.getCollection('MuteAudit').find({"GameId":2109,"Unit" : 1000,"ActionType" : 1,"Reason" : {$regex:/自动禁言}})

 

Query Module (field names) to "British Open" data header

db.getCollection('Goods').find({"Module": /^英/})

 

The query specifies column GoodsId GoodsName data

db.getCollection('Goods').find({},{GoodsId:1,GoodsName:1});  

 

The query specifies column GoodsId GoodsName data, GoodsId> = 1 GoodsId <= 6

db.getCollection('Goods').find({GoodsId:{'$gte':1,'$lte':6}},{GoodsId:1,GoodsName:1});  

 

1 according GoodsId ascending sort descending -1

db.getCollection('Goods').find().sort({GoodsId : 1}); 
db.getCollection('Goods').find().sort({GoodsId : -1});

db.getCollection('Goods').find().sort({"GoodsId ":-1,'_id':1})

 

When the query GoodsName = data 40039 of the crystal GoodsId =

db.getCollection('Goods').find({"GoodsId" : 40039, "GoodsName ":"时之晶"}); 

 

Keyword or query 

db.getCollection('Goods').find({$or:[{GoodsId: 1}, {GoodsId:5}]});

 

Keyword limit before the inquiry 5

db.getCollection('Goods').find().limit(5);

 

10 After the query keywords skip

db.getCollection('Goods').find().skip(10)

 

According to statistics conditions

db.getCollection('Goods').find().count();

 

Update

A data update

 db.getCollection('Message').update({"ServerId" : 3326},{$set:{"Content" : "888888888"}})

 

Multi-condition batch update multiple data

db.getCollection('MuteAudit').find({"GameId":2109,"Unit" : 1000,"RoleId" : 723129}).forEach(
   function(item){                
       db.getCollection('MuteAudit').update({"_id" : item._id},{$set:{ "ActionType":0}})
   }
)

 

delete

Deletes the specified column data

db.getCollection('Goods').remove({"_id" : 1});

 

New

New data

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/shiyilang398/p/11368214.html