mongodb---增删改查

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014453898/article/details/84064313

(一)增(往集合中插入数据,即文档)

第一种插入方法:

往集合textCollection中插入数据 {”name“:”xiaoming“},直接用 db.集合名.insert():

> db.textCollection.insert({"name":"xiaoming"})
WriteResult({ "nInserted" : 1 })
>

查看集合textCollection中的文档(数据):

> db.textCollection.find()
{ "_id" : ObjectId("5beb9f0c114e061388762deb"), "name" : "xiaoming" }
>

一次性插入多个数据:

> db.textCollection.insert([{a:1},{b:2}])

第二种插入方法:

先构建一个变量(document)表示要插入的数据,然后用  db.集合名.insert(变量)来插入:

> document=({name:"xiaohong",age:"21"})
{ "name" : "xiaohong", "age" : "21" }
> db.textCollection.insert(document)
WriteResult({ "nInserted" : 1 })
> db.textCollection.find()
{ "_id" : ObjectId("5beb9f0c114e061388762deb"), "name" : "xiaoming" }
{ "_id" : ObjectId("5bebea60127c93733825f9ff"), "name" : "xiaohong", "age" : "21" }
>

一次性插入多个数据:

> documents = ([{name:"xiaoming",age:21},{name:"xiaowen",age:22}])
> db.textCollection.insert(documents)

(二)删

删除集合textCollection中,所有name为xiaoming的文档:

> db.textCollection.remove({name:"xiaoming"})
WriteResult({ "nRemoved" : 2 })

第二行表示成功删除了2条文档。

(三)查

(1)查询整个集合:

db.textCollection.find():查询整个textCollection集合:

> db.textCollection.find()
{ "_id" : ObjectId("5bec01c8127c93733825fa0a"), "name" : "xiaoming", "age" : 21 }
{ "_id" : ObjectId("5bec01c8127c93733825fa0b"), "name" : "xiaohong", "age" : 20 }
>

(2)db.集合名.find().pretty() ----格式化输出:

> db.textCollection.find().pretty()
{
        "_id" : ObjectId("5bec01c8127c93733825fa0a"),
        "name" : "xiaoming",
        "age" : 21
}
{
        "_id" : ObjectId("5bec01c8127c93733825fa0b"),
        "name" : "xiaohong",
        "age" : 20
}
>

(3)条件并(AND)查找:

查找textCollection集合中age为21且name为xiaoming的文档:

> db.textCollection.find({"age":21,"name":"xiaoming"})
{ "_id" : ObjectId("5bec01c8127c93733825fa0a"), "name" : "xiaoming", "age" : 21 }
>

(4)条件或(OR)查找:

查找textCollection集合中age为20或21的文档:

用到 $or:表示“或”。

> db.textCollection.find({$or:[{"age":20},{"age":21}]})
{ "_id" : ObjectId("5bec01c8127c93733825fa0a"), "name" : "xiaoming", "age" : 21 }
{ "_id" : ObjectId("5bec01c8127c93733825fa0b"), "name" : "xiaohong", "age" : 20 }
{ "_id" : ObjectId("5bec0439127c93733825fa0c"), "name" : "xiaowen", "age" : 21 }
{ "_id" : ObjectId("5bec0439127c93733825fa0d"), "name" : "xiaogang", "age" : 20 }
>

(四)改

改即update,更新集合中的文档。

把textCollection集合中的包含name为xiaoming的数据的age字段改成18:

db.textCollection.update({"name":"xiaoming"},{$set:{"age":18}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>

上面的方法只能修改第一个找到的数据,若有多个数据的name为xiaoming,且要把所有name为xiaoming的数据的age字段改为18的话,要额外加上{multi:true}:

> db.textCollection.update({"name":"xiaoming"},{$set:{"age":18}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.textCollection.find()
{ "_id" : ObjectId("5bec01c8127c93733825fa0a"), "name" : "xiaoming", "age" : 18 }
{ "_id" : ObjectId("5bec01c8127c93733825fa0b"), "name" : "xiaohong", "age" : 20 }
{ "_id" : ObjectId("5bec0439127c93733825fa0c"), "name" : "xiaowen", "age" : 21 }
{ "_id" : ObjectId("5bec0439127c93733825fa0d"), "name" : "xiaogang", "age" : 20 }
{ "_id" : ObjectId("5bec092c127c93733825fa0e"), "name" : "xiaoming", "age" : 18 }
>

猜你喜欢

转载自blog.csdn.net/u014453898/article/details/84064313