猫鼬:如何使用聚合并一起找到

Col*_*amp 22 mongoose mongodb node.js mongodb-query aggregation-framework

我如何在Mongoose中使用聚合find一起?
即我有以下架构:

const schema = new Mongoose.Schema({
  created: { type: Date, default: Date.now() },
  name: { type: String, default: 'development' }
  followers: [{ type: Mongoose.Schema.ObjectId, ref: 'Users'}]
...
})

export default Mongoose.model('Locations', schema)
Run Code Online (Sandbox Code Playgroud)

如何仅使用字段name和查询用户followers_count.
followers_count:长度followers.

在那里,我知道我们可以使用select来获得该字段name.
我们怎么能算得上followers

chr*_*dam 25

对于MongoDB 3.6及更高版本,请使用$expr允许在查询语言中使用聚合表达式的运算符:

var followers_count = 30;
db.locations.find({
   "$expr": { 
       "$and": [
           { "$eq": ["$name", "development"] },
           { "$gte": [{ "$size": "$followers" }, followers_count ]}
       ]
    }
});
Run Code Online (Sandbox Code Playgroud)

对于不兼容的版本,您可以使用$match$redact管道来查询集合.例如,如果要查询locations名称为"development"且followers_count大于30的集合,请运行以下聚合操作:

const followers_count = 30;
Locations.aggregate([
    { "$match": { "name": "development" } },
    {
        "$redact": {
            "$cond": [
                { "$gte": [ { "$size": "$followers" }, followers_count ] },
                "$$KEEP",
                "$$PRUNE"
            ]
        }
    }
]).exec((err, locations) => {
    if (err) throw err;
    console.log(locations);
})
Run Code Online (Sandbox Code Playgroud)

或者在一个管道内

Locations.aggregate([
    {
        "$redact": {
            "$cond": [
                { 
                    "$and": [
                        { "$eq": ["$name", "development"] },
                        { "$gte": [ { "$size": "$followers" }, followers_count ] }
                     ]
                },
                "$$KEEP",
                "$$PRUNE"
            ]
        }
    }
]).exec((err, locations) => {
    if (err) throw err;
    console.log(locations);
})
Run Code Online (Sandbox Code Playgroud)

以上将返回仅_id包含用户引用的位置.要返回用户文档作为"填充"关注者数组的方法,您可以追加$lookup管道.


如果底层Mongo服务器版本是3.4及更高版本,则可以将管道作为

let followers_count = 30;
Locations.aggregate([
    { "$match": { "name": "development" } },
    {
        "$redact": {
            "$cond": [
                { "$gte": [ { "$size": "$followers" }, followers_count ] },
                "$$KEEP",
                "$$PRUNE"
            ]
        }
    },
    {
        "$lookup": {
            "from": "users",
            "localField": "followers",
            "foreignField": "_id",
            "as": "followers"
        }
    }
]).exec((err, locations) => {
    if (err) throw err;
    console.log(locations);
})
Run Code Online (Sandbox Code Playgroud)

否则你需要$unwind在申请之前使用追随者数组$lookup,然后在之后重新组合$group管道:

let followers_count = 30;
Locations.aggregate([
    { "$match": { "name": "development" } },
    {
        "$redact": {
            "$cond": [
                { "$gte": [ { "$size": "$followers" }, followers_count ] },
                "$$KEEP",
                "$$PRUNE"
            ]
        }
    },
    { "$unwind": "$followers" },
    {
        "$lookup": {
            "from": "users",
            "localField": "followers",
            "foreignField": "_id",
            "as": "follower"
        }
    },
    { "$unwind": "$follower" },
    {
        "$group": {
            "_id": "$_id",
            "created": { "$first": "$created" },
            "name": { "$first": "$name" },
            "followers": { "$push": "$follower" }
        }
    }
]).exec((err, locations) => {
    if (err) throw err;
    console.log(locations);
})
Run Code Online (Sandbox Code Playgroud)


Isr*_*inc 10

您可以使用如下:

db.locations.aggregate([
  {$match:{"your find query"}},
  {$project:{"your desired fields"}}
])
Run Code Online (Sandbox Code Playgroud)

在比赛中你可以做以下事情:

{{$match:{name:"whatever"}}
Run Code Online (Sandbox Code Playgroud)

在项目中,您可以使用数字0或1选择所需的字段,如:

{$project:{_id:1,created:0,name:1}}
Run Code Online (Sandbox Code Playgroud)

其中0表示不放1表示放置.