美文网首页我爱编程
mongodb文档操作

mongodb文档操作

作者: 非文666 | 来源:发表于2018-02-24 16:55 被阅读0次

插入文档

语法

mongodb使用insert() 或save()方法插入文档,语法如下:

db.COLLECTION_NAME.insert(document)

实例

# 存储{"name":"wfei"}到myCollection 集合中
> db.myCollection.insert({"name":"wfei"})
WriteResult({ "nInserted" : 1 })
> db.myCollection.find()
{ "_id" : ObjectId("5a90238c113385ab59f380b0"), "name" : "wfei" }

注:如果myCollection集合不在数据库中,会自动创建该集合
我们也可以将数据定义为一个变量,如下所示:

# 定义document变量
> document=({"name":"ccc"});
{ "name" : "ccc" }

然后执行插入操作

> db.myCollection.insert(document)
WriteResult({ "nInserted" : 1 })
> db.myCollection.find()
{ "_id" : ObjectId("5a90238c113385ab59f380b0"), "name" : "wfei" }
{ "_id" : ObjectId("5a9024cc113385ab59f380b1"), "name" : "ccc" }

更新文档

语法

update()语法用于更新已经存在的文档,语法如下:

db.COLLECTION_NAME.update(
<query>,
<update>,
{
  upsert:<boolean>,
  multi:<boolean>,
  writeConcern:<document>
}
)

参数说明:

  • <query> update的查询条件
  • <update> update的对象和一些更新的操作符,如$,$inc
  • upsert 可选 ,这个参数的意思是,如果where条件下不存在值,是否执行插入操作,默认是false
  • multi 可选,mongodb默认false,只更新找到的第一条记录,如果设置为true,就把找到的所有记录都更新
  • writeConcern 可选,抛出异常的级别

实例

# 更新name值为ddd的文档的age字段为19
> db.myCollection.update({"name":"ddd"},{$set:{"age":19}},{upsert:true})
WriteResult({
    "nMatched" : 0,
    "nUpserted" : 1,
    "nModified" : 0,
    "_id" : ObjectId("5a902bced5f9a583d52a7f42")
})
## myCollection集合中不存在name值为ddd的文档,所以添加到集合中
> db.myCollection.find()
{ "_id" : ObjectId("5a90238c113385ab59f380b0"), "name" : "wfei" }
{ "_id" : ObjectId("5a9024cc113385ab59f380b1"), "name" : "ccc" }
{ "_id" : ObjectId("5a902bced5f9a583d52a7f42"), "name" : "ddd", "age" : 19 }
# 将name为ddd的文档age更新为27
> db.myCollection.update({"name":"ddd"},{$set:{"age":27}},{multi:true})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.myCollection.find()
{ "_id" : ObjectId("5a90238c113385ab59f380b0"), "name" : "wfei" }
{ "_id" : ObjectId("5a9024cc113385ab59f380b1"), "name" : "ccc" }
{ "_id" : ObjectId("5a902bced5f9a583d52a7f42"), "name" : "ddd", "age" : 27 }
#将name为ddd的文档age增加27
> db.myCollection.update({"name":"ddd"},{$inc:{"age":27}},{multi:true})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.myCollection.find()
{ "_id" : ObjectId("5a90238c113385ab59f380b0"), "name" : "wfei" }
{ "_id" : ObjectId("5a9024cc113385ab59f380b1"), "name" : "ccc" }
{ "_id" : ObjectId("5a902bced5f9a583d52a7f42"), "name" : "ddd", "age" : 54 }

另外对于upsert、multi两个属性可以简写,只写true、false就可以第一个boolean值代表upsert,第二个boolean代表multi值

> db.myCollection.update({"name":"eee"},{$set:{"address":"万年花城"}},true,false)
WriteResult({
    "nMatched" : 0,
    "nUpserted" : 1,
    "nModified" : 0,
    "_id" : ObjectId("5a902e8bd5f9a583d52a7f5c")
})
> db.myCollection.find()
{ "_id" : ObjectId("5a90238c113385ab59f380b0"), "name" : "wfei" }
{ "_id" : ObjectId("5a9024cc113385ab59f380b1"), "name" : "ccc" }
{ "_id" : ObjectId("5a902bced5f9a583d52a7f42"), "name" : "ddd", "age" : 54 }
{ "_id" : ObjectId("5a902e8bd5f9a583d52a7f5c"), "name" : "eee", "address" : "万年花城" }

删除文档

语法

mongodb删除文档使用remove,语法如下

db.COLLECTION_NAME.remove(
  <query>,
  <justone>
)

参数说明:

  • query 可选,删除的条件
  • justone 可选,如果选择1或者true,则只删除一个文档

实例

#  删除name为ccc的文档
> db.myCollection.remove({"name":"ccc"},{justone:true})
WriteResult({ "nRemoved" : 1 })

同样上面的删除语句可以简写

#  删除name为ccc的文档
> db.myCollection.remove({"name":"ccc"},1)
WriteResult({ "nRemoved" : 1 })

删除集合中所有的文档数据

如果要删除整个集合的文档数据

语法

可以使用以下语法:

db.COLLECTION_NAME.remove({})

实例

> db.myCollection.remove({})
WriteResult({ "nRemoved" : 3 })
> db.myCollection.find()
> 

相关文章

网友评论

    本文标题:mongodb文档操作

    本文链接:https://www.haomeiwen.com/subject/nqjfxftx.html