Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mongoose如何在嵌入式数组中选择特定对象_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js Mongoose如何在嵌入式数组中选择特定对象

Node.js Mongoose如何在嵌入式数组中选择特定对象,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我一直在四处寻找,仍然找不到正确的答案 基本上,我在mongodb中有以下数据模型 { "_id": { "$oid": "565db83bcd7bef020b1bdae8" }, "userId": { "$oid": "5653a3267827f178214918fc" }, "targetUser": [ { "userId": { "$oid": "564663a5c0aefc151625b5e2" },

我一直在四处寻找,仍然找不到正确的答案

基本上,我在mongodb中有以下数据模型

{
"_id": {
    "$oid": "565db83bcd7bef020b1bdae8"
},
"userId": {
    "$oid": "5653a3267827f178214918fc"
},
"targetUser": [
    {
        "userId": {
            "$oid": "564663a5c0aefc151625b5e2"
        },
        "status": "Approved",
        "_id": {
            "$oid": "565db83bcd7bef020b1bdaea"
        }
    },
    {
        "userId": {
            "$oid": "564548249bb75c600ff94cdd"
        },
        "status": "Sent",
        "_id": {
            "$oid": "565db884cd7bef020b1bdaed"
        }
    }
]
},
{
"_id": {
    "$oid": "565db884cd7bef020b1bdaec"
},
"userId": {
    "$oid": "564548249bb75c600ff94cdd"
},
"targetUser": [
    {
        "userId": {
            "$oid": "5653a3267827f178214918fc"
        },
        "status": "Pending",
        "_id": {
            "$oid": "565db884cd7bef020b1bdaee"
        }
    }
]
}
我怎样才能只返回以下数据

{
"userId": {
    "$oid": "5653a3267827f178214918fc"
},
"targetUser": [
    {
        "userId": {
            "$oid": "564663a5c0aefc151625b5e2"
        },
        "status": "Approved",
        "_id": {
            "$oid": "565db83bcd7bef020b1bdaea"
        }
    },
}
我使用了下面的代码,但结果是返回null

Connection.aggregate([
    {$match:{userId:'5653a3267827f178214918fc'}},
    {$unwind:'$targetUser'},
    {$match:{'targetUser.status':'Approved'}},
    {$group:{
        _id:'$_id',
        targetUser:{
            $push:{
                status:'$targetUser.status'
            }
        }
    }}
],function(err,connections){
    console.log(connections);
    console.log(err);
})

非常感谢您的帮助

据我所知,您不需要进行聚合,一个带有投影参数的简单函数将返回您需要的内容

Connection.findOne({
    userId: new ObjectId("5653a3267827f178214918fc")
}).select({
    targetUser: {
        $elemMatch: { status: "Approved" }
    }
}).exec(callback);
但是,如果您的字段多于
userId
targetUser
,则需要将它们添加到第二个参数中,如下所示

Connection.findOne({
    userId: new ObjectId("5653a3267827f178214918fc")
}).select({
    targetUser: {
        $elemMatch: { status: "Approved" }
    },
    oneMoreField: 1,
    anotherField: 1
}).exec(callback);

你好ShanShan,我尝试了以下操作,但仍然没有填充任何内容,
Connection.find({userId:newobjectid(“5653a327827f178214918fc”)},{targetUser:{$elemMatch:{status:“Approved”}}}).exec(函数(err,connections){console.log(connections)console.log(err)});
Connection代表什么?我假设它是您正在查询的集合,但我猜不是。您能否编辑您的问题并添加有关
Connection
的代码(删除任何个人信息)?这与猫鼬的句法有关,我正在编辑我的答案。