简体   繁体   English

如何在猫鼬中添加运算符

[英]How to add operators in mongoose

How i can use operators like $max $min with mongoose 我如何在猫鼬中使用像$max $min这样的运算符

I tried 我试过了

 User.find({Followers:max});

but that didn't work 但这没用

You can use the .aggregate() method 您可以使用.aggregate()方法

db.collection.aggregate([ 
    { "$group": { 
        "_id": null,
        "max": { "$max": "$Followers" }, 
        "min": { "$min": "$Followers" } 
    }}
])

The _id should be null otherwise the query will only return max/min for each group _id应该为null,否则查询将仅返回每个组的最大/最小

$max and $min operators will not work with .find(), you need to use .aggregate() for it to work. $ max和$ min运算符不能与.find()一起使用,您需要使用.aggregate()使其起作用。

    user.aggregate([ 
          { "$group": { 
                "_id": null,
                "maxfollowers": { "$max": "$Followers" }, 
                "minfollowers": { "$min": "$Followers" } 
          }}
       ])

_id field is mandatory in the $group and to find the max/min values for Followers for the entire collection not for special group it needs to be set to null otherwise if you want the result for each group you can use that field name instead of null eg _id字段在$ group中是必填字段,要查找整个集合(而不是特殊组)的Followers的最大值/最小值,需要将其设置为null,否则,如果您希望每个组的结果都可以使用该字段名称代替空例如

    user.aggregate([ 
      { "$group": { 
             "_id": "$name",
             "maxfollowers": { "$max": "$Followers" }, 
             "minfollowers": { "$min": "$Followers" } 
          }}
     ])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM