在MongooseJS中将$ in与嵌套对象一起使用 [英] Using $in in MongooseJS with nested objects

查看:91
本文介绍了在MongooseJS中将$ in与嵌套对象一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的mongo数组仅保存ID时,我已经在节点Web服务中成功使用$ in.这是示例数据.

I've been successfully using $in in my node webservice when my mongo arrays only held ids. Here is sample data.

{
    "_id": {
        "$oid": "52b1a60ce4b0f819260bc6e5"
    },
    "title": "Sample",
    "team": [
        {
            "$oid": "52995b263e20c94167000001"
        },
        {
            "$oid": "529bfa36c81735b802000001"
        }
    ],
    "tasks": [
        {
            "task": {
                "$oid": "52af197ae4b07526a3ee6017"
            },
            "status": 0
        },
        {
            "task": {
                "$oid": "52af197ae4b07526a3ee6017"
            },
            "status": 1
        }
    ]
}

请注意,tasks是一个数组,但id嵌套在"task"中,而在团队中,它位于顶层.这是我的问题所在.

Notice that tasks is an array, but the id is nested in "task", while in teams it is on the top level. Here is where my question is.

在我的Node路由中,这通常是我在项目中调用ID数组的方式,这在团队示例中可以很好地工作,但显然不适用于我的任务示例.

In my Node route, this is how I typically deal with calling a array of IDs in my project, this works fine in the team example, but obviously not for my task example.

app.get('/api/tasks/project/:id', function (req, res) {
    var the_id = req.params.id;
    var query = req.params.query;
    models.Projects.findById(the_id, null, function (data) {

        models.Tasks.findAllByIds({
            ids: data._doc.tasks,
            query: query
        }, function(items) {

            console.log(items);
            res.send(items);
        });
    });
});

与我的模型通信,该模型具有一个名为findAllByIds的方法

That communicates with my model which has a method called findAllByIds

module.exports = function (config, mongoose) {
    var _TasksSchema = new mongoose.Schema({});

    var _Tasks = mongoose.model('tasks', _TasksSchema);

    /*****************
     *    Public API
     *****************/
    return {

        Tasks: _Tasks,


        findAllByIds: function(data, callback){
            var query = data.query;

            _Tasks.find({_id: { $in: data.ids} }, query, function(err, doc){
                callback(doc);
            });
        }
    }

}

在此呼叫中,我有 $ in:data.ids ,它可以在简单的数组中工作,例如上面的"teams"示例.像任务"示例一样,一旦嵌套了对象,它就不再起作用,并且我不确定如何指定$ in来查看data.ids数组,而是使用任务"值.

In this call I have $in: data.ids which works in the simple array like the "teams" example above. Once I nest my object, as with "task" sample, this does not work anymore, and I am not sure how to specify $in to look at data.ids array, but use the "task" value.

我想避免必须遍历数据以创建仅具有id的数组,然后在返回数据后重新填充其他值,除非这是唯一的选择.

I'd like to avoid having to iterate through the data to create an array with only id, and then repopulate the other values once the data is returned, unless that is the only option.

更新

我曾想过要像这样设置我的mongo文档,尽管我仍然想知道如何以其他方式进行操作,以防将来无法实现.

I had a thought of setting up my mongo document like this, although I'd still like to know how to do it the other way, in the event this isn't possible in the future.

"tasks": {
            "status0": [
            {
                "$oid": "52995b263e20c94167000001"
            },
            {
                "$oid": "529bfa36c81735b802000001"
            }
        ],
            "status1": [
            {
                "$oid": "52995b263e20c94167000001"
            },
            {
                "$oid": "529bfa36c81735b802000001"
            }
        ]
        }

推荐答案

您可以在tasks数组上调用map将其投影到仅包含ObjectId值的新数组中:

You can call map on the tasks array to project it into a new array with just the ObjectId values:

models.Tasks.findAllByIds({
    ids: data.tasks.map(function(value) { return value.task; }),
    query: query
}, function(items) { ...

这篇关于在MongooseJS中将$ in与嵌套对象一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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