mongo

查询

> db.tianyc04.find()
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 2, "fruit" : [ "apple", "orange", "peach" ] }
{ "_id" : 3, "fruit" : [ "orange", "banana", "peach" ] }

#通过全匹配,查询第一行
> db.tianyc04.find({fruit:["apple", "banana", "peach"]})
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }

#如果将数组中的顺序颠倒,则第一行就匹配不上了。此时可以使用$all

> db.tianyc04.find({fruit:["apple", "peach", "banana"]})

> db.tianyc04.find({fruit:{$all:["apple", "peach", "banana"]}})
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }

#也可以只输入一个元素进行查询

 > db.tianyc04.find({fruit:'apple'})

{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 2, "fruit" : [ "apple", "orange", "peach" ] }

#如果这个元素变成了数组,mongo就会进行精确匹配。此时你可能需要使用$all进行模糊匹配:

> db.tianyc04.find({fruit:['apple']})

> db.tianyc04.find({fruit:{$all:['apple']}})
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 2, "fruit" : [ "apple", "orange", "peach" ] }

模糊查询

db.student.find({name:/joe/})
或者
db.student.find({name:{$regex:/joe/}})

当使用正则时候,还可以为$regex操作符中的option选项进行设置,可以改变正则匹配的默认行为,有i, m, x以及S四个选项。

更新

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

你可能感兴趣的:(mongo)