猫鼬:将addToSet与ObjectIds一起使用会导致孤立ID [英] Mongoose: Using addToSet with ObjectIds Results in Orphan Id

查看:75
本文介绍了猫鼬:将addToSet与ObjectIds一起使用会导致孤立ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用mongoDB的$ addToSet到充满ObjectIds的数组时,我遇到了一个相当有趣的问题.

I am having a rather interesting problem using mongoDB's $addToSet to an array full of ObjectIds.

在我的猫鼬模式(发生")中,我声明了一个名为"expected"的ObjecIds数组,供.populate()使用.

In my mongoose schema ("Happening"), I declare an array of ObjecIds called "expected", to be used by .populate().

expected: [{type: Schema.Types.ObjectId, ref: "User" }]

...在我使用它的任何地方都可以很好地工作.到目前为止一切顺利.

... which works nicely everywhere I use it. So far so good.

然后我尝试使用$ addToSet来更新Happening.expected数组,如下所示:

I then attempt to update the Happening.expected array using $addToSet as outlined here:

http://docs.mongodb.org/manual/reference/operator/addToSet/

像这样:

app.get("/happening/yamobethere/:id", ensureLoggedIn("/login"),
    function (req, res) {

    // userId is the mongo ObjectId of the user record
    var userId = req.session.user.id, 
        eventId = req.params.id;

    models.Happening.update(
        {_id: eventId}, {
            $addToSet: {expected: userId}
        },
        function(err, updated){
            if (err) {
                res.json({"error": err});
            }
            res.json({"updated": updated});
    });
});

...总是产生:

{updated: 1}

现在,文档使我期望传入的实际userId,因此"1"有点奇怪.我以为这会失败,并且鉴于接下来发生的怪异,这似乎是mongodb的某种错误,渗入了我的结果.

Now the docs lead me to expect the actual userId that I passed in, so the "1" is a bit odd. I expected it to be a fail, and in light of the weirdness that happens next, it appears to be a mongodb error of some sort percolating it's way back to me as results.

奇怪的是,当我检查数据库时,我发现确实添加了一个新的ObjectId:只是没有传入的那个.

The weirdness is, when I check my database, I see that indeed a new ObjectId has been added: just not the one I passed in.

"expected" : [
        ObjectId("51cb18623ade2b9f1e000004"), 
        ObjectId("51cdb7c12f0e58bdb3000001")
    ],

成为

"expected" : [
        ObjectId("51cb18623ade2b9f1e000004"), 
        ObjectId("51cdb7c12f0e58bdb3000001"), 
        ObjectId("51cdb80e09612bfab3000002")  
    ],

新的ObjectId没有出现在我的任何集合中.它似乎是个孤儿,但我是mongo noob,所以我对此可能堆肥了.

The new ObjectId does not appear in any of my collections. It appears to be an orphan, but I'm a mongo noob, so I may be full of compost on this.

我确实尝试将userId转换为ObjectId:

I did attempt to cast the userId as an ObjectId:

$addToSet: {expected: mongoose.Types.ObjectId.fromString(userId)}

但是这并没有改变,实际上并没有必要,因为架构应该处理它.

but that changed nothing, and really should not be necessary, since the schema should handle it.

我真的不希望下载整个对象,将值附加到"expected"数组中,然后将整个schmear发回以进行更新.

I'd really rather not resort to downloading the entire object, appending the value to the "expected" array, then sending the whole schmear back for an update.

任何人都感激不尽,伙计们.谢谢!

Any help appreciated, folks. Thanks!

更新:

一位同事建议使用以下技术:

A colleague suggested the following technique:

    var addMe = {$addToSet: {expected: userId}};

    models.Happening.findByIdAndUpdate(eventId, addMe, function(err, me) {
        if (err) {
            return json(err);
        }
        res.json(200, me);
    });

...有点改进,因为它实际上返回了一个对象供我检查.不幸的是,这还会导致孤立的ObjecId出现在数组中,而不是我指定的现有userId值.

... which is a bit of an improvement, since it actually returns an object for me to inspect. Unfortunately, it also results in orphaned ObjecIds appearing in the array, rather than the existing userId value I specified.

再次感谢!

推荐答案

看来,我的护照策略正在通过oauth的数据返回被拒绝尝试在db中创建新用户的ObjectID.所以,代码很好,我的数据是垃圾.

It appears that my passport strategy is returning the ObjectID of the rejected attempted creation of a new user in the db via data from oauth. So, the code is fine, my data is garbage.

从不信任任何事物,并准备看起来像个布布. :-)

Never trust anything, and be prepared to look like a boob. :-)

感谢澄清我的返回值JohnnyHK.

Thanks for the clarification on my return values JohnnyHK.

这篇关于猫鼬:将addToSet与ObjectIds一起使用会导致孤立ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆