代码之家  ›  专栏  ›  技术社区  ›  Julioarhernandez

在mongodb数据库中的对象数组上按id查找

  •  4
  • Julioarhernandez  · 技术社区  · 8 年前

    我试图在对象数组中查找对象的id。该id与文档中的其他id具有相同的字段名\u id。 这是我的模型(简介)

    var CardSchema = new mongoose.Schema({
      beName: String,
      beLink: String,
      cards: [{ 
        cardType: String,
        cardBundle: String
      }]
    

    这是我的数据库内容示例

    _id: ObjectId(5a52540638086448bf4235e8)
    beName: Name1
    beLink: Link1
    cards: Array
     0: Object
        cardType: type1
        cardBundle: 1
        _id: ObjectId(5a526749d0ddab4bcdcc1556)
     1: Object
        cardType: type2
        cardBundle: 1
        _id: ObjectId(5a526749d0ddab4bcdcc1557)
    
    ...
    
    _id: ObjectId(5a52540638086448bf4235e9)
    beName: Namex
    beLink: Linkx
    cards: Array
     0: Object
        cardType: typex
        cardBundle: x
        _id: ObjectId(5a526749d0ddab4bcdcc1598)
     1: Object
        cardType: type2
        cardBundle: 1
        _id: ObjectId(5a526749d0ddab4bcdcc1599)
    

    我想找到一张像这样的卡的id

    Cards.find({ _id: req.params.id}, function (err, post) {
        if (err) return next(err);
        res.json(post);
      });
    

    但我得到的结果是空的

    我也试过了

    Cards.find({ _id: new ObjectId(req.params.id)}...
    
    3 回复  |  直到 8 年前
        1
  •  3
  •   Saravana    8 年前

    您可能需要使用 aggregate 功能到 $unwind 用于查找匹配卡片的卡片阵列 _id .

    所以,用猫鼬代替 find 使用 总数的 管道

    示例文档

    > db.cards.findOne()
    {
        "_id" : ObjectId("5a52f4136fe82b42b7439a21"),
        "beName" : "Name1",
        "beLink" : "Link1",
        "cards" : [
            {
                "cardType" : "type1",
                "cardBundle" : 1,
                "_id" : "5a52f3a66f112b42b7439a20"
            },
            {
                "cardType" : "type2",
                "cardBundle" : 1,
                "_id" : "5a52f3a66f112b42b7439a21"
            }
        ]
    }
    

    聚合函数

    > db.cards.aggregate([{$unwind: "$cards"}, {$match:{"cards._id" : "5a52f3a66f112b42b7439a20"}}] )
    

    结果文档

    > db.cards.aggregate([{$unwind: "$cards"}, {$match:{"cards._id" : "5a52f3a66f112b42b7439a20"}}] ).pretty()
    {
        "_id" : ObjectId("5a52f4136fe82b42b7439a21"),
        "beName" : "Name1",
        "beLink" : "Link1",
        "cards" : {
            "cardType" : "type1",
            "cardBundle" : 1,
            "_id" : "5a52f3a66f112b42b7439a20"
        }
    }
    > 
    

    如果您了解家长,可以进一步优化 _id号 ,在骨料管道中 $match 按父级 _id号 然后 $展开 然后 $匹配 在阵列卡上 _id号

    > db.cards.aggregate([{$match:{"_id":ObjectId("5a52f4136fe82b42b7439a21")}},{$unwind: "$cards"}, {$match:{"cards._id" : "5a52f3a66f112b42b7439a20"}}] )
    
        2
  •  2
  •   Arsalan Etehad    4 年前

    这个问题有点模糊,但通过查看其他答案并在url中包含卡的id,我猜您已经有了卡的id,并且希望找到父文档。如果是这样,则不需要聚合。查找查询执行以下操作:

    db.cards.find({"cards._id": _id})
        3
  •  0
  •   Ajay Gupta jkhouw1    8 年前

    尝试这样做:

    const ObjectId = require("mongodb").ObjectID,
    
    /* some other statements */
    
    let cardId = new ObjectId(req.params.id)
    Cards.find({ _id: cardId}, function (err, post) {
        if (err) return next(err);
        res.json(post);
    });
    

    供参考: https://mongodb.github.io/node-mongodb-native/api-bson-generated/objectid.html