0

I have a collection invites which i am using to store some invite data.Below are two rows of data i captured from my collection invites

{
        "_id" : "n4nECpQFnBFiKkt6N",
        "createdAt" : ISODate("2016-10-11T11:59:16.988Z"),
        "lat" : -1.2861292178882062,
        "lng" : 36.89101696014404,
        "eventop" : "0800123456",
        "eventtitle" : "test2",
        "eventtime" : ISODate("2016-10-11T11:59:16.988Z"),
        "invited" : "0800123456",
        "hour" : "0",
        "minute" : "1",
        "pmam" : "pm",
        "eventduration" : "60",
        "eventtype" : "4",
        "eventcategory" : "hackathon",
        "eventstatus" : 11
}


{
        "_id" : "LtFB4WMu4xGC7Kyxf",
        "createdAt" : ISODate("2016-10-11T11:59:16.939Z"),
        "lat" : -1.284970795243023,
        "lng" : 36.89183235168457,
        "primaryowner" : "0800123456",
        "eventtitle" : "test2",
        "eventtime" : ISODate("2016-10-11T11:59:16.939Z"),
        "hour" : "0",
        "minute" : "1",
        "pmam" : "pm",
        "eventduration" : "60",
        "eventtype" : "4",
        "eventcategory" : "hackathon",
        "eventstatus" : 11
}

I want to fetch some data from the collections invites. This is the data i want to get.

I want to get data(all fields) where invited field is 0800123456 and eventtype is 3 and eventstatus is 11. I also want to get data where eventtype is 4 where eventstatus is 4 and the field primaryowner exists.

db.invites.find({invited:"0720069005",eventtype:"3"},{eventstatus:"11"},{$and: [{eventtype: "4"},{eventstatus:"11"},{primaryowner: { $exists: true }}]}).pretty()

This is my query.It returns some data but i am not sure whether its correct.

1

From your question, I couldnot clearly understand what is the required business scenario, but with the sample data provided I have given an pseudo code and the respective mongodb implementation of that pseudo code.

if invited field is "0800123456" 
   if event type is 3 and event status is 11
     select matching documents
or if event type is 4 and event status is 11 and primary owner exists
     select matching documents

Mongo DB Query

db.collection.find({
  $or: [
    {
      $and: [
        {
          "invited": "0800123456"
        },
        {
          "eventtype": {
            $eq: "3"
          }
        },
        {
          "eventstatus": {
            $eq: 11
          }
        }
      ]
    },
    {
      $and: [
        {
          "eventtype": {
            $eq: "4"
          }
        },
        {
          "eventstatus": {
            $eq: 11
          }
        },
        {
          primaryowner: {
            $exists: true
          }
        }
      ]
    }
  ]
})

If this is not matching your scenario, then write your own query with the help of this sample code.

Hope it Helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.