我如何在mongoose 中同时使用aggregatefind

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)

How can I query the users with only the fields name and followers_count.
followers_count: the length of followers .

在那里,我知道我们可以用select来得到name

推荐答案

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

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

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

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);
})

或者在单个管道内

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);
})

上面的内容只会返回用户提供的_id条参考信息.要将用户文档作为"填充"关注者数组的手段返回,可以附加100管道.


如果基础Mongo server版本为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);
})

否则,在应用101之前,您需要使用100跟随者数组,然后使用102管道重新组合:

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);
})

Mongodb相关问答推荐

在一个视图中连接两个集合MongoDB;展开有什么作用?

Mongo 将一个数组链接到另一个数组

发布到 MongoDB 时生成的附加id字段

MongoDB:通过嵌套数组中的最后一个元素值获取文档

Mongo:查找具有 0 个关联文档的文档,成本更低

有没有一种方法可以找到一个文档并通过 Go 更改 mongodb 中的 id/value 来克隆它

有没有办法从另一条记录中插入一条记录

MongoDB:如何将所有文档合并到聚合管道中的单个文档中

带有 mongodb 和 nodejs 的实时 Web 应用程序

如何按季度分组日期?

MongoDB:多个 $elemMatch

带有部分字符串的mongoose文本搜索

如何使用 Pymongo 在 MongoDB 中 Select 单个字段?

如何仅通过一次调用将一组对象保存到mongoose数据库?

PyMongo 中的警告消息:count is deprecated

缩短 MongoDB 属性名称值得吗?

spring 数据MongoDB.生成id的错误

使用 C# 聚合 $lookup

MongoError:Can't extract geo keys from object

用 MongoDB 中的属性表示多对多关系的最佳模型