简体   繁体   English

MongoDB-如何从集合中查询嵌入式文档

[英]MongoDB - How to query Embedded Documents from a collection

Gurus - I'm stuck in a situation that I can't figure out how I can query from the following collection " users ", it has 2 embedded documents " signup " and " activity ": 专家-我陷入一种无法解决如何从以下集合“ 用户 ”中查询的情况,它具有2个嵌入式文档“ signup ”和“ activity ”:

{
    "appid": 2,
    "userid": 404915,
    "signup": {
        "dt": "2010-12-28",
        "platform": 2 
    },
    "activity": {
        {
            "dt": "2010-12-28",
            "platform": 3,
            "login_count": 8,
            "game_completed": 13 
        },
        {
            "dt": "2010-12-30",
            "platform": 3,
            "login_count": 8,
            "game_completed": 13 
        } ,
        {
            "dt": "2010-12-31",
            "platform": 3,
            "login_count": 8,
            "game_completed": 13 
        } 
    }
},{"appid":2,"userid":404915...}

I need to query: 我需要查询:

unique logins of users who signed up between Date and Date+7 and logged in within Date 在Date和Date + 7之间注册并在Date中登录的用户的唯一登录名

Then: 然后:

Unique logins of users who signed up between Date and Date+7, and logged in between Date+7 and Date+14 在Date和Date + 7之间注册并在Date + 7和Date + 14之间登录的用户的唯一登录名

PLEASE PLEASE Guide me how I can achieve this any example/sample? 请指导我如何实现任何示例/示例? based on this will be really helpful :-) 基于此将非常有帮助:-)

Thanks a lot! 非常感谢!

Here is how you get the result for your first query: 这是您如何获得第一个查询的结果的方法:

var start = new Date(2010, 11, 25);
var end = new Date(2010, 12, 1);

db.users.distinct("userid", {"signup.dt" : {$gte: start, $lte: end},
      "activity" : {"$elemMatch" : { dt: {$gte: start, $lte: end}}}});

The second is like it with adding 7 days to the start and end date to the dates after activity. 第二个类似,将开始日期和结束日期增加了7天,之后又增加了活动日期。

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

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