MongoDB-具有嵌套字段的操作

MongoDB-具有嵌套字段的操作,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我的twitter数据如下所示: db.users.findOne() { "_id" : ObjectId("578ffa8e7eb9513f4f55a935"), "user_name" : "koteras", "retweet_count" : 0, "tweet_followers_count" : 461, "source" : "<a href=\"http://twitter.com/download/iphone\" rel=\"n

我的twitter数据如下所示:

db.users.findOne()
{
    "_id" : ObjectId("578ffa8e7eb9513f4f55a935"),
    "user_name" : "koteras",
    "retweet_count" : 0,
    "tweet_followers_count" : 461,
    "source" : "<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>",
    "coordinates" : null,
    "tweet_mentioned_count" : 1,
    "tweet_ID" : "755891629932675072",
    "tweet_text" : "RT @ochocinco: I beat them all for 10 straight hours #FIFA16KING",
    "user" : {
        "CreatedAt" : ISODate("2011-12-27T09:04:01Z"),
        "FavouritesCount" : 5223,
        "FollowersCount" : 461,
        "FriendsCount" : 619,
        "UserId" : 447818090,
        "Location" : "501"
    }
db.users.findOne()
{
“_id”:ObjectId(“578FFA8E7EB9513F45A935”),
“用户名”:“koteras”,
“转发计数”:0,
“推特粉丝数”:461,
“来源”:“,
“坐标”:空,
“tweet_提到了_计数”:1,
“tweet_ID”:“755891629932675072”,
“推文”:“RT@ochocinco:我连续10个小时击败他们#FIFA16KING”,
“用户”:{
“CreatedAt”:ISODate(“2011-12-27T09:04:01Z”),
“收藏夹”:5223,
“跟随者计数”:461,
“Friendsont”:619,
“用户ID”:447818090,
“位置”:“501”
}
例如,我想查找“FollowersCount”大于“FavoriteSCount”的用户数。我如何才能做到这一点?

运营商是专门为此而设计的

db.users.find( { $where: function() { return (this.user.FollowersCount > this.user.FavouritesCount) } } );
但请记住,这将运行单线程JS代码,并且速度会较慢

另一种选择是使用聚合管道投影差异,然后在差异上使用
$match

db.users.aggregate([
  {$project: {
    diff: {$subtract: ["$user.FollowersCount", "$user.FavouritesCount"]},
    // project remaining fields here
    }
  },
  {$match: {diff: {$gt: 0}}}
])
根据我的经验,我发现第二个比第一个快得多。

该操作符是专门为此设计的

db.users.find( { $where: function() { return (this.user.FollowersCount > this.user.FavouritesCount) } } );
但请记住,这将运行单线程JS代码,并且速度会较慢

另一种选择是使用聚合管道投影差异,然后在差异上使用
$match

db.users.aggregate([
  {$project: {
    diff: {$subtract: ["$user.FollowersCount", "$user.FavouritesCount"]},
    // project remaining fields here
    }
  },
  {$match: {diff: {$gt: 0}}}
])

根据我的经验,我发现第二个要比第一个快得多。

要获得“FollowersCount”大于“FavoriteSunt”的用户数量,可以使用聚合框架,该框架包含一些可以应用的运算符

考虑第一个用例,该用例着眼于操作管道内的比较运算符,以及后续的管道,以基于值过滤文档。然后,您可以通过应用聚合过滤文档的管道来获得最终用户计数:

db.users.aggregate([
    {
        "$project": {               
            "hasMoreFollowersThanFavs": { 
                "$cmp": [ "$user.FollowersCount", "$user.FavouritesCount" ]
            }
        }
    },
    { "$match": { "hasMoreFollowersThanFavs": 1 } },    
    {
        "$group": {
            "_id": null,
            "count": { "$sum": 1 }
        }
    }
])

另一个选项是使用带有运算符的单个管道,该管道包含了上述的功能,并使用系统变量返回与指定条件匹配的所有文档,并使用系统变量丢弃不匹配的文档:

db.collection.aggregate([
    {
        "$redact": {
            "$cond": [
                { 
                    "$eq": [
                        { "$cmp": [ "$user.FollowersCount", "$user.FavouritesCount" ] }, 
                        1
                    ]
                }, 
                "$$KEEP", 
                "$$PRUNE"
            ]
        }
    },  
    {
        "$group": {
            "_id": null,
            "count": { "$sum": 1 }
        }
    }
])

要获得“FollowersCount”大于“FavoriteSCount”的用户数,可以使用聚合框架,该框架包含一些可以应用的运算符

考虑第一个用例,该用例着眼于操作管道内的比较运算符,以及后续的管道,以基于值过滤文档。然后,您可以通过应用聚合过滤文档的管道来获得最终用户计数:

db.users.aggregate([
    {
        "$project": {               
            "hasMoreFollowersThanFavs": { 
                "$cmp": [ "$user.FollowersCount", "$user.FavouritesCount" ]
            }
        }
    },
    { "$match": { "hasMoreFollowersThanFavs": 1 } },    
    {
        "$group": {
            "_id": null,
            "count": { "$sum": 1 }
        }
    }
])

另一个选项是使用带有运算符的单个管道,该管道包含了上述的功能,并使用系统变量返回与指定条件匹配的所有文档,并使用系统变量丢弃不匹配的文档:

db.collection.aggregate([
    {
        "$redact": {
            "$cond": [
                { 
                    "$eq": [
                        { "$cmp": [ "$user.FollowersCount", "$user.FavouritesCount" ] }, 
                        1
                    ]
                }, 
                "$$KEEP", 
                "$$PRUNE"
            ]
        }
    },  
    {
        "$group": {
            "_id": null,
            "count": { "$sum": 1 }
        }
    }
])

在这两种情况下,对返回的游标应用
itcount()
以获取匹配文档的计数Hanks man!那么“And”函数呢?如果我想查找包含特定文本和特定位置的推文,我尝试了这个方法,但不起作用b.users.find({$And[{“user.location”:“501”},{tweet_text:/UEFA/})在这种情况下,Thr不需要
$和
workdb.users.find({“user.Location”:“501”,tweet_text:/UEFA/})
也应该起作用,在这两种情况下,对返回的光标应用
itcount()
以获得匹配文档的计数汉克斯曼!那么“and”呢功能?如果我想查找包含特定文本和特定位置的推文。我尝试了这个,但它不起作用。查找({$和[{“user.location”:“501”},{tweet_text:/UEFA/}]})在这方面不需要
$和
workdb.users.find({“user.location”:“501”,tweet_text:/UEFA/})
也应该起作用