美文网首页
MongoDB | 4. 文档(数据)查询

MongoDB | 4. 文档(数据)查询

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

    查询文档(数据)

    查询所有

    > 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
    }
    

    联合查询文档(数据)

    插入数据

    db.user.insertMany([
        {
            _id: ObjectId("5af2b2c6b138c267e414c072"),
            uid: "uid000",
            name: "小红",
            age: 26
        },
        {
            _id: ObjectId("5af2b2c6b138c267e414c073"),
            uid: "uid001",
            name: "小芳",
            age: 27
        }
    ]);
    
    db.order.insertMany([
        {
            _id: ObjectId("4af2b2c6b138c267e414c071"),
            uid: "uid000",
            product: "产品1",
            money: 100
        },
        {
            _id: ObjectId("4af2b2c6b138c267e414c072"),
            uid: "uid000",
            product: "产品2",
            money: 200
        },
        {
            _id: ObjectId("4af2b2c6b138c267e414c073"),
            uid: "uid001",
            product: "产品1",
            money: 100
        },
        {
            _id: ObjectId("4af2b2c6b138c267e414c074"),
            uid: "uid001",
            product: "产品2",
            money: 200
        }
    ]);
    

    联合查询

    > db.user.aggregate([{$lookup: { from: "order", localField: "uid",  foreignField: "uid",  as: "orders"}}]).pretty()
    {
            "_id" : ObjectId("5af2b2c6b138c267e414c072"),
            "uid" : "uid000",
            "name" : "小红",
            "age" : 26,
            "orders" : [
                    {
                            "_id" : ObjectId("4af2b2c6b138c267e414c071"),
                            "uid" : "uid000",
                            "product" : "产品1",
                            "money" : 100
                    },
                    {
                            "_id" : ObjectId("4af2b2c6b138c267e414c072"),
                            "uid" : "uid000",
                            "product" : "产品2",
                            "money" : 200
                    }
            ]
    }
    {
            "_id" : ObjectId("5af2b2c6b138c267e414c073"),
            "uid" : "uid001",
            "name" : "小芳",
            "age" : 27,
            "orders" : [
                    {
                            "_id" : ObjectId("4af2b2c6b138c267e414c073"),
                            "uid" : "uid001",
                            "product" : "产品1",
                            "money" : 100
                    },
                    {
                            "_id" : ObjectId("4af2b2c6b138c267e414c074"),
                            "uid" : "uid001",
                            "product" : "产品2",
                            "money" : 200
                    }
            ]
    }
    

    拆分orders

    > db.user.aggregate([
    ...   { $lookup: { from: "order", localField: "uid", foreignField: "uid", as: "orders"}},
    ...   { $unwind: { path: "$orders", preserveNullAndEmptyArrays: true }}
    ... ]).pretty()
    {
            "_id" : ObjectId("5af2b2c6b138c267e414c072"),
            "uid" : "uid000",
            "name" : "小红",
            "age" : 26,
            "orders" : {
                    "_id" : ObjectId("4af2b2c6b138c267e414c071"),
                    "uid" : "uid000",
                    "product" : "产品1",
                    "money" : 100
            }
    }
    {
            "_id" : ObjectId("5af2b2c6b138c267e414c072"),
            "uid" : "uid000",
            "name" : "小红",
            "age" : 26,
            "orders" : {
                    "_id" : ObjectId("4af2b2c6b138c267e414c072"),
                    "uid" : "uid000",
                    "product" : "产品2",
                    "money" : 200
            }
    }
    {
            "_id" : ObjectId("5af2b2c6b138c267e414c073"),
            "uid" : "uid001",
            "name" : "小芳",
            "age" : 27,
            "orders" : {
                    "_id" : ObjectId("4af2b2c6b138c267e414c073"),
                    "uid" : "uid001",
                    "product" : "产品1",
                    "money" : 100
            }
    }
    {
            "_id" : ObjectId("5af2b2c6b138c267e414c073"),
            "uid" : "uid001",
            "name" : "小芳",
            "age" : 27,
            "orders" : {
                    "_id" : ObjectId("4af2b2c6b138c267e414c074"),
                    "uid" : "uid001",
                    "product" : "产品2",
                    "money" : 200
            }
    }
    

    分组求和

    > db.user.aggregate([
    ...   { $lookup: { from: "order", localField: "uid", foreignField: "uid", as: "orders" }},
    ...   { $unwind: { path: "$orders", preserveNullAndEmptyArrays: true }},
    ...   { $group: { _id: "$_id", name: { $first: "$name" }, age: { $first: "$age" }, money: { $sum: "$orders.money" }}}
    ... ]).pretty()
    {
            "_id" : ObjectId("5af2b2c6b138c267e414c072"),
            "name" : "小红",
            "age" : 26,
            "money" : 300
    }
    {
            "_id" : ObjectId("5af2b2c6b138c267e414c073"),
            "name" : "小芳",
            "age" : 27,
            "money" : 300
    }
    

    联合查询后,独立显示数据项

    >db.order.aggregate([
      { $lookup: { from: "user", localField: "uid", foreignField: "uid", as: "u" }}, 
      { $unwind: "$u" }, 
      { $addFields: { name: "$u.name" }}, 
      { $project: { _id: 1, product: 1, money: 1, name: 1 }}
    ]).pretty()
    

    相关文章

      网友评论

          本文标题:MongoDB | 4. 文档(数据)查询

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