1

hello i am new to mongodb syntax i achived the result below using a query:

   {
    "_id" : "abc",
    "info" : [ 
        {
            "user_status" : true,
            "timeActive" : 2.0
        }, 
        {
            "user_status" : false,
            "timeActive" : 1.0
        }
    ]
}

the data in by database is given below

/* 1 */
{
    "_id" : ObjectId("60f25f41f6d12949670676e4"),
    "user_id" : "xyz",
    "from_time" : 12.0,
    "to_time" : 13.0,
    "status" : true
}

/* 2 */
{
    "_id" : ObjectId("60f25f41f6d12949670676e5"),
    "user_id" : "xyz",
    "from_time" : 13.0,
    "to_time" : 14.0,
    "status" : false
}

/* 3 */
{
    "_id" : ObjectId("60f25f41f6d12949670676e6"),
    "user_id" : "xyz",
    "from_time" : 14.0,
    "to_time" : 15.0,
    "status" : true
}

/* 4 */
{
    "_id" : ObjectId("60f25f41f6d12949670676e7"),
    "user_id" : "xyz",
    "from_time" : 15.0,
    "to_time" : 17.0,
    "status" : false
}

/* 5 */
{
    "_id" : ObjectId("60f27483f6d12949670676e8"),
    "user_id" : "abc",
    "from_time" : 15.0,
    "to_time" : 17.0,
    "status" : true
}

/* 6 */
{
    "_id" : ObjectId("60f27483f6d12949670676e9"),
    "user_id" : "abc",
    "from_time" : 17.0,
    "to_time" : 18.0,
    "status" : false
}

the query i used is

db.users.aggregate(
[
    {$match:{ status_to_time : {$ne:null} }},
    {$set:{totalTime:   {$subtract:["$status_to_time" , "$status_from_time"]} }},
    
    {$group:{_id:{user_id:"$information_id" , user_status:"$status"} , allSum:{$sum:"$totalTime"} }},
    
    
    
   {$project:{"user_id":"$_id.user_id" ,"user_status":"$_id.user_status" , timeA:"$allSum",_id:0}},
    
    {$sort:{user_id:1 , user_status:-1}},
    
    {$group:{_id:"$user_id" , info:{$push:{user_status:"$user_status" , timeActive:"$timeA"}} }},]
    )

but i also want to to achieve division as info[0].timeActive / info[1].timeActive in a parmeter timeDiv so my final result of query should be

    {
        "_id" : "abc",
        "info" : [ 
            {
                "user_status" : true,
                "timeActive" : 2.0
            }, 
            {
                "user_status" : false,
                "timeActive" : 1.0
            }
        ]
timeDiv:2 // division as info[0].timeActive / info[1].timeActive
    }

please someone answer how to achieve that

2
  • How is your data? Can you add an input example? Commented Jul 31, 2021 at 17:47
  • Thanks for your response i got my answer but i am providing my data here in case it help any one else the data is given now in my question Commented Aug 1, 2021 at 6:17

1 Answer 1

1

You could use $addField, $divide and $arrayElemAt like this: playground.

Just add this aggregation at the end of your query and you should get the expected result.

Sign up to request clarification or add additional context in comments.

2 Comments

However, be carefull with the object index positions and 0, null values, etc
Thanks for your answer you solved my problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.