Mongodb 多条件查询的投影

Mongodb 多条件查询的投影,mongodb,Mongodb,我想根据几个条件查询文档,然后只保留特定字段。以下查询(mongoskin语法)返回当前用户的TODO,并按标记进行筛选: db.collection('users').find({ _id : db.bson_serializer.ObjectID.createFromHexString(req.user._id.toString()) }, { todos : { $elemMatch : { tags : filterTag

我想根据几个条件查询文档,然后只保留特定字段。以下查询(mongoskin语法)返回当前用户的TODO,并按标记进行筛选:

db.collection('users').find({
    _id : db.bson_serializer.ObjectID.createFromHexString(req.user._id.toString())
}, {
    todos : {
        $elemMatch : {
            tags : filterTag
        }
    }
}
然后我尝试添加投影,但过滤不再进行

db.collection('users').find({
    _id : db.bson_serializer.ObjectID.createFromHexString(req.user._id.toString()),
    todos : {
        $elemMatch : {
            tags : filterTag
        }
    }
}, {
    _id : 0,
    'todos.value' : 1,
    'todos._id' : 1
}

实际上,我找到了聚合的解决方案:

db.collection('users').aggregate({
    $unwind : '$todos'
}, {
    $match : {
        _id : db.bson_serializer.ObjectID.createFromHexString(req.user._id.toString()),
        'todos.tags' : filterTag
    }
}, {
    $project : {
        _id : 0,
        'todos.value' : 1,
        'todos._id' : 1
    }
}