mongodb---增删改查

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

第一种插入方法:

往集合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 }
>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值