美文网首页
MongoDB | 3. 文档(数据)操作

MongoDB | 3. 文档(数据)操作

作者: ShadowFieldEric | 来源:发表于2021-01-05 16:13 被阅读0次

插入数据

插入一条文档(数据)

> db.demo_collection.insert({
... "name":"coco",
... "type": "drink",
... "count": 100
... })
WriteResult({ "nInserted" : 1 })

> db.demo_collection.insertOne({
... "name": "一点点",
... "type": "drink",
... "count": 50
... })
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5fdc465c54e1efc76f83e0a3")
}

插入多条文档(数据)

> db.demo_collection.insertMany([{"name":"Lee"},{"name":"Chen"}])
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("5fdc478154e1efc76f83e0a7"),
                ObjectId("5fdc478154e1efc76f83e0a8")
        ]
}

删除文档(数据)

remove命令

> db.demo_collection.remove({"name":"Lee"})
WriteResult({ "nRemoved" : 2 })
> db.demo_collection.find()
{ "_id" : ObjectId("5fdbfdd854e1efc76f83e0a0"), "name" : "Eric" }
{ "_id" : ObjectId("5fdc016454e1efc76f83e0a1"), "demo" : "Demo" }
{ "_id" : ObjectId("5fdc458554e1efc76f83e0a2"), "name" : "coco", "type" : "drink", "count" : 100 }
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50 }
{ "_id" : ObjectId("5fdc477a54e1efc76f83e0a6"), "name" : "Chen" }

deleteOne命令

> db.demo_collection.find({date:"2020-12-20"})
{ "_id" : ObjectId("5fdc458554e1efc76f83e0a2"), "name" : "coco", "type" : "drink", "count" : 100, "date" : "2020-12-20" }
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50, "date" : "2020-12-20" }
{ "_id" : ObjectId("5fdc478154e1efc76f83e0a8"), "name" : "Zhang", "type" : "human", "date" : "2020-12-20" }

> db.demo_collection.deleteOne({date:"2020-12-20"}) 
{ "acknowledged" : true, "deletedCount" : 1 }

> db.demo_collection.find({date:"2020-12-20"})
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50, "date" : "2020-12-20" }
{ "_id" : ObjectId("5fdc478154e1efc76f83e0a8"), "name" : "Zhang", "type" : "human", "date" : "2020-12-20" }

deleteMany命令

> db.demo_collection.find({date:"2020-12-20"})
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50, "date" : "2020-12-20" }
{ "_id" : ObjectId("5fdc478154e1efc76f83e0a8"), "name" : "Zhang", "type" : "human", "date" : "2020-12-20" }

> db.demo_collection.deleteMany({date:"2020-12-20"})
{ "acknowledged" : true, "deletedCount" : 2 }

> db.demo_collection.find({date:"2020-12-20"})
>

更新文档(数据)

  1. 单行更新
> db.demo_collection.find()
...
{ "_id" : ObjectId("5fdc478154e1efc76f83e0a8"), "name" : "Chen" }
...

> db.demo_collection.update({"_id": ObjectId("5fdc478154e1efc76f83e0a8")}, {$set:{"name":"Zhang", "type":"human"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.demo_collection.find()
...
{ "_id" : ObjectId("5fdc478154e1efc76f83e0a8"), "name" : "Zhang", "type" : "human" }
...
  1. 更新所有符合条件的数据
> db.demo_collection.find({name: {$exists: true, $not: {$size: 0}}})
{ "_id" : ObjectId("5fdbfdd854e1efc76f83e0a0"), "name" : "Eric" }
{ "_id" : ObjectId("5fdc458554e1efc76f83e0a2"), "name" : "coco", "type" : "drink", "count" : 100 }
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50 }
{ "_id" : ObjectId("5fdc478154e1efc76f83e0a8"), "name" : "Zhang", "type" : "human" }

> db.demo_collection.update({name: {$exists: true, $not: {$size: 0}}}, {$set: {"date": "2020-12-20"}}, false, true)
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 3 })

> db.demo_collection.find({name: {$exists: true, $not: {$size: 0}}})
{ "_id" : ObjectId("5fdbfdd854e1efc76f83e0a0"), "name" : "Eric", "date" : "2020-12-20" }
{ "_id" : ObjectId("5fdc458554e1efc76f83e0a2"), "name" : "coco", "type" : "drink", "count" : 100, "date" : "2020-12-20" }
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50, "date" : "2020-12-20" }
{ "_id" : ObjectId("5fdc478154e1efc76f83e0a8"), "name" : "Zhang", "type" : "human", "date" : "2020-12-20" }
  1. 更新符合条件的第一条
> db.demo_collection.find({name: {$exists: true, $not: {$size: 0}}})
{ "_id" : ObjectId("5fdbfdd854e1efc76f83e0a0"), "name" : "Eric", "date" : "2020-12-20" }
{ "_id" : ObjectId("5fdc458554e1efc76f83e0a2"), "name" : "coco", "type" : "drink", "count" : 100, "date" : "2020-12-20" }
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50, "date" : "2020-12-20" }
{ "_id" : ObjectId("5fdc478154e1efc76f83e0a8"), "name" : "Zhang", "type" : "human", "date" : "2020-12-20" }

> db.demo_collection.update({name: {$exists: true, $not: {$size: 0}}}, {$set: {"date": "2020-12-21"}}, false, false)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.demo_collection.find({name: {$exists: true, $not: {$size: 0}}})
{ "_id" : ObjectId("5fdbfdd854e1efc76f83e0a0"), "name" : "Eric", "date" : "2020-12-21" }
{ "_id" : ObjectId("5fdc458554e1efc76f83e0a2"), "name" : "coco", "type" : "drink", "count" : 100, "date" : "2020-12-20" }
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50, "date" : "2020-12-20" }
{ "_id" : ObjectId("5fdc478154e1efc76f83e0a8"), "name" : "Zhang", "type" : "human", "date" : "2020-12-20" }

查询文档(数据)

查询所有

> db.demo_collection.find()
{ "_id" : ObjectId("5fdbfdd854e1efc76f83e0a0"), "name" : "Eric" }
{ "_id" : ObjectId("5fdc016454e1efc76f83e0a1"), "demo" : "Demo" }
{ "_id" : ObjectId("5fdc458554e1efc76f83e0a2"), "name" : "coco", "type" : "drink", "count" : 100 }
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50 }
{ "_id" : ObjectId("5fdc477a54e1efc76f83e0a5"), "name" : "Lee" }
{ "_id" : ObjectId("5fdc477a54e1efc76f83e0a6"), "name" : "Chen" }

查询name为"一点点"的数据

> db.demo_collection.find({"name":"一点点"})
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50 }

使用and条件查询,查询name为"一点点"并且type为"drink"的数据

db.demo_collection.find({"name":"一点点", "type": "drink"})
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50 }

使用or条件查询

> db.demo_collection.find({$or:[{"name":"Lee"},{"name":"coco"}]})
{ "_id" : ObjectId("5fdc458554e1efc76f83e0a2"), "name" : "coco", "type" : "drink", "count" : 100 }
{ "_id" : ObjectId("5fdc477a54e1efc76f83e0a5"), "name" : "Lee" }

使用大于、大于等于,小于、小于等于,不等于查询

> db.demo_collection.find({"count":{$lt:50}})
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50 }
> db.demo_collection.find({"count":{$gte:50}})
{ "_id" : ObjectId("5fdc458554e1efc76f83e0a2"), "name" : "coco", "type" : "drink", "count" : 100 }
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50 }

> db.demo_collection.find({"count":{$lt:100}})
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50 }
> db.demo_collection.find({"count":{$lte:100}})
{ "_id" : ObjectId("5fdc458554e1efc76f83e0a2"), "name" : "coco", "type" : "drink", "count" : 100 }
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50 }

> db.demo_collection.find({"count":{$ne:100}})
{ "_id" : ObjectId("5fdbfdd854e1efc76f83e0a0"), "name" : "Eric" }
{ "_id" : ObjectId("5fdc016454e1efc76f83e0a1"), "demo" : "Demo" }
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50 }
{ "_id" : ObjectId("5fdc478154e1efc76f83e0a8"), "name" : "Zhang", "type" : "human" }

查询name中包含"点"

> db.demo_collection.find({"name": /点/}))
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50 }

查询name中以"一"开头

> db.demo_collection.find({"name": /^一/})
{ "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"), "name" : "一点点", "type" : "drink", "count" : 50 }

使用pretty()链式方法,可使数据按格式化显示

> db.demo_collection.find({"name":"一点点"}).pretty()
{
        "_id" : ObjectId("5fdc46ab54e1efc76f83e0a4"),
        "name" : "一点点",
        "type" : "drink",
        "count" : 50
}

相关文章

网友评论

      本文标题:MongoDB | 3. 文档(数据)操作

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