1:插入数据稍微复杂的形式
doc=( {"user_id" : "ABCDBWN", "password" :"ABCDBWN" , "date_of_join" : "15/10/2010" , "education" :"B.C.A." , "profession" : "DEVELOPER", "interest" :"MUSIC", "community_name" :["MODERN MUSIC", "CLASSICALMUSIC", "WESTERN MUSIC"], "community_moder_id" : ["MR. BBB", "MR. JJJ", "MRMMM"], "community_members" : [500, 200, 1500], "friends_id" :["MMM123", "NNN123", "OOO123"], "ban_friends_id" :["BAN123", "BAN456", "BAN789"] });
> db.person.insert(doc) WriteResult({ "nInserted" : 1 })
2:利用for循环批量插入文档,上一篇博客已经说过。
3:条件查询
'>'对应'$gt',
'<'对应'$lt',
'>='对应'$gte'
'<='对应'$lte',
'!='对应'$ne',
'='对应'没有关键字'
例如:查询uid>98的数据
> db.person.find({"uid" : {$gt : 98}}) { "_id" : ObjectId("55471dfc994c2a0fdbfa128b"), "uid" : 100, "addr" : "shanghai" } { "_id" : ObjectId("55471dfc994c2a0fdbfa128c"), "uid" : 99, "addr" : "shanghai" } >
例如查询uid不等于99的数据量
> db.person.find({"uid":{$ne : 99}}).count() 100
还有$in,$nin,等
例如:查询uid=100并且addr="shanghai"的文档
> db.person.find({"uid" : 100,"addr":"shanghai"}) { "_id" : ObjectId("55471dfc994c2a0fdbfa128b"), "uid" : 100, "addr" : "shanghai" } >
查询 uid=100 或者 addr="shanghia"的文档
db.person.find({$or:[{},{}]})
> db.person.find({$or:[{"uid":100},{"addr":"shanghai"}]}).count()
查询 addr 以 s开头的文档:
> db.person.find({"addr" : /^s/})
查询addr一i结尾的文档:
> db.person.find({"addr":/i$/})
使用where条件查询
> db.person.find({$where:function(){return this.uid==88}}) { "_id" : ObjectId("55471dfc994c2a0fdbfa1297"), "uid" : 88, "addr" : "shanghai" } >