1

I have a document like this

{
    "status": {
        "current": 0,
        "priority": [{
            "operationName": "PHOTO",
            "status": "WAITING"
        },
        {
            "operationName": "DESIGN",
            "status": "NOTARRIVED"
        },
        {
            "operationName": "COLOR_SEPARATION",
            "status": "NOTARRIVED"
        }]
    }
}

and want to query on data like this

{
    "status.priority.$status.current.operationName": {
        $in: ['SERVICES', 'PHOTO']
    }
}

when I query like this

{
    "status.priority.0.operationName": {
        $in: ['SERVICES', 'PHOTO']
    }
}

it returns the data needed as the 'PHOTO' is the current operation.

I need to query based on an index of the array and this index is stored in the document in status.current

any hint?

UPDATE After question solved I want to optimize it.

2
  • 1
    Please be more specific what you want to achieve Commented Oct 5, 2018 at 12:05
  • want to query on some index and that index stored in db in "$status.current" I want to use this index in my query like "status.priority.3.operationName" Commented Oct 5, 2018 at 12:07

2 Answers 2

2

You can use $arrayElemAt with $expr in 3.6.

Something like

db.colname.find(
 {"$expr":{
   "$in":[
     {"$arrayElemAt":["$status.priority.operationName","$status.current"]},
     ['DESIGN', 'COLOR_SEPARATION', 'PHOTO']
   ]
 }}
)
Sign up to request clarification or add additional context in comments.

1 Comment

How to optimize it using an index?
1

For this you need to use aggregation

db.collname.aggregate([{
    "$project": {
      "_id": 1,
      priority: { $arrayElemAt: ["$status.priority", '$status.current'] },
    }
  },
  {
    $match: {
      "priority.operationName": {
        $in: ['DESIGN', 'COLOR_SEPARATION', 'PHOTO']
      }

    }
  }
])

This will work for you.

Result will be like

{
    "_id" : ObjectId("5b6b656818883ec018d1542d"),
    "priority" : {
        "operationName" : "PHOTO",
        "status" : "WAITING"
    }
}

1 Comment

How to optimize it using an index?

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.