1

I am new to mongoDB . I have aware of some basic queries of mongoDb but not advanced. My document looks like following :

{
    "_id" : ObjectId("5289deaa84aedcc100228259"),
    "gender" : "male",
    "intrest" : [
        {
            "userId" : ObjectId("5286294984ae18ac5d19af36"),
            "timestamp" : ISODate("2013-11-18T09:32:38.040Z"),
            "status" : "Pending"
        },
        {
            "userId" : ObjectId("5286295984ae18ac5d19af37"),
            "timestamp" : ISODate("2013-11-18T09:33:17.493Z"),
            "status" : "Pending"
        }
    ],
    "intrestAccepted" : [ ],
    "intrestCancled" : [ ],
    "intrestDeclined" : [ ],
    "intrestReceived" : [
        ObjectId("5286294984ae18ac5d19af36"),
        ObjectId("5286295984ae18ac5d19af37")
    ],
    "owner" : ObjectId("5286293284ae18ac5d19af35"),
    "postDesc" : "gggg",
    "tags" : [
        "ggg"
    ]
}
{

    "_id" : ObjectId("5289dea084aedcc100228258"),
    "gender" : "female",
    "intrest" : [
        {
            "userId" : ObjectId("5286294984ae18ac5d19af36"),
            "timestamp" : ISODate("2013-11-18T09:32:42.934Z"),
            "status" : "Pending"
        },
        {
            "userId" : ObjectId("5286295984ae18ac5d19af37"),
            "timestamp" : ISODate("2013-11-18T09:33:18.520Z"),
            "status" : "Pending"
        }
    ],
    "intrestAccepted" : [ ],
    "intrestCancled" : [ ],
    "intrestDeclined" : [ ],
    "intrestReceived" : [
        ObjectId("5286294984ae18ac5d19af36"),
        ObjectId("5286295984ae18ac5d19af37")
    ],
    "owner" : ObjectId("5286293284ae18ac5d19af35"),
    "postDesc" : "asdf",
    "tags" : [
        "asdf"
    ]
}
{

    "_id" : ObjectId("5289dec984aedcc10022825a"),
    "gender" : "male",
    "intrest" : [
        {
            "userId" : ObjectId("5286295984ae18ac5d19af37"),
            "timestamp" : ISODate("2013-11-18T09:33:20.996Z"),
            "status" : "Pending"
        }
    ],
    "intrestAccepted" : [ ],
    "intrestCancled" : [ ],
    "intrestDeclined" : [ ],
    "intrestReceived" : [
        ObjectId("5286295984ae18ac5d19af37")
    ],
    "owner" : ObjectId("5286294984ae18ac5d19af36"),
    "postDesc" : "fff",
    "tags" : [
        "fff"
    ]
}

I want to find totalcount for intrestReceived[] whose owner is ObjectId("5286293284ae18ac5d19af35")

What query am I supposed to write. Please help .

chridam
  • 100,957
  • 23
  • 236
  • 235
Sumit D
  • 401
  • 4
  • 15

1 Answers1

0

You could try something simple like this:

intrestReceived_length = 0
db.col.find({ _id: ObjectId("5286293284ae18ac5d19af35") }).forEach( function( doc ){
  intrestReceived_length = doc.intrestReceived.length;
});

The find will extract only the document that matches the ObjectId you provided and then we use a forEach on the cursor to iterate over all of the (one) results. Within the forEach callback, we just use a JavaScript array.length command to extract the size of the array.

You could also output only the intrestReceived array by providing a projection in your find command:

db.col.find({ _id: ObjectId("5286293284ae18ac5d19af35") }, { "intrestReceived": 1 })...

This will return the same document but it will only contain the _id and intrestReceived attributes.

Lix
  • 47,311
  • 12
  • 103
  • 131