文章目录
  1. 1. mongodb-client
  2. 2. syntax

mongodb-client

1
2
3
4
5
mongo mongodb://[xxx]:[xxx]@127.0.0.1:[port]/[database name]?authSource=admin
show databases
use [database name]
show collections
db.[collection name].find({'[key]':'[value]'}, {"[key].[child key].[chile key...]":1})

syntax

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//db.foo.update({'_id':ObjectId("5978778eb99d221c140017f2")}, {$set:{'gender':0}})
// db.foo.find({}, {rolename:1, _id:0}).limit(10).skip(1).sort({rolename:1}).explain()
// db.foo.aggregate([ // pipe
// {$match:{level:{$gt:10, $lte:60}}},
// {$group:{_id:"$level", count:{$sum:1}}},
// {$sort:{count:-1}}
// ])
db.foo.find({flist:{$exists:1}}) // find data that contains key 'flist'
db.foo.find({roleid:'xxx'}, {'flist.tid':1}) // grab target piece of data from the result
/*
{
"_id" : ObjectId("5a0ae334b99d220460003b6f"),
"flist" : [
{
"tid" : 5100001,
// other keys blocked by the {'flist.tid':1} option
},
{
"tid" : 5100098,
// other keys blocked by the {'flist.tid':1} option
}
],
// other keys blocked by the {'flist.tid':1} option
}
*/
db.foo.find({roleid:'xxx'}, {'flist.tid':1}).length() // find returns a iterater, length() function return the number of mathced results(its not affected by {'flist.tid':1} option)
// 1
var a = db.foo.findOne({roleid:'xxx'}, {'flist.tid':1}) // findOne returns one doc
print(a.flist.length) // the array('flist') contains 'length' property
// 2
文章目录
  1. 1. mongodb-client
  2. 2. syntax