【问题标题】:How to use aggregrate in mongodb to $match _id如何在 mongodb 中使用聚合来 $match _id
【发布时间】:2015-12-28 21:17:14
【问题描述】:

文档:

{
    "_id" : ObjectId("560c24b853b558856ef193a3"),
    "name" : "Karl Morrison",
    "pic" : "",
    "language" : ObjectId("560c24b853b558856ef193a2"),
    "cell" : 1,
    "local" : {
        "email" : "karl.morrison@instanty.se",
        "password" : "12345"
    },
    "sessions" : [
        {
            "id" : ObjectId("560c24b853b558856ef193a5")
        }
    ]
}

这行得通:

yield new Promise(function (resolve, reject) {
                users.col.aggregate([
                        {
                            $match: {
                                'name': 'Karl Morrison'
                            }
                        }
                    ],
                    function (err, res) {
                        console.log('err ' + err);
                        console.log('res ' + JSON.stringify(res)); // <-- echos the object retrieved
                        if (err === null)
                            resolve(res);
                        reject(err);
                    });
            });

这不起作用:

yield new Promise(function (resolve, reject) {
                users.col.aggregate([
                        {
                            $match: {
                                '_id': '560c24b853b558856ef193a3' // <-- _id of the user
                            }
                        }
                    ],
                    function (err, res) {
                        console.log('err ' + err);
                        console.log('res ' + JSON.stringify(res));
                        if (err === null)
                            resolve(res);
                        reject(err);
                    });
            });

.col 访问本机 mongodb 对象(否则使用 co-monk)。所以我是手动做的。然而,这不起作用。我怀疑我没有将 id 十六进制字符串转换为 ObjectId。无论我尝试什么都没有效果。

【问题讨论】:

  • _id 必须是 ObjectId。 Monk 在与.aggregate() 一起使用时无法从字符串中为您转换类型,因此您需要自己转换类型。
  • MongoDB - Aggregate $match on ObjectId 的可能重复项。基本相同的原因是“自己投射”,因为两个框架都使用相似的代码进行自动投射,但都不适用于.aggregate()
  • @BlakesSeven 确实是,谢谢,找了好久(可能是猫鼬没找到)

标签: mongodb


【解决方案1】:
const ObjectId = mongoose.Types.ObjectId;
const User = mongoose.model('User')

User.aggregate([
  {
    $match: { _id: ObjectId('560c24b853b558856ef193a3') }
  }
])

【讨论】:

  • 如果我使用新的 ObjectId 而不是只使用 ObjectId 会有问题吗?
  • 谢谢一群人。 mongoose.Types.ObjectId 拯救了我的一天。
  • 天哪,这节省了我宝贵的时间,谢谢
  • 有谁明白为什么匹配你需要指定 ObjectId 并且你可以直接做 MyModel.find({ destination: myDestination.id }) 之类的东西???提前谢谢你
【解决方案2】:

使用 toString() 方法

const User = mongoose.model('User')
User.aggregate([
  {
    $match: { _id: user_id.toString()  }
  }
]

【讨论】:

  • 这不是与正确的做法相反吗?
【解决方案3】:

试试这个

const User = require('User')
const mongoose = require("mongoose");


User.aggregate([
  {
    $match: { _id: new mongoose.Types.ObjectId('560c24b853b558856ef193a3') }
  }
])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2015-05-25
    • 2013-04-21
    • 2013-03-10
    • 1970-01-01
    相关资源
    最近更新 更多