1

I have a Mongodb document that contains an array of bookings like this:

{ id: myId,
  bookings: [
    {station: "Nurse"},
    {station: "Doctor"},
    {station: "Pharmacist"}
  ]
}

I have a view for each station where they see all records that are booked next. So for the Nurse, this would be: find({"bookings.0.station": "Nurse"}), returning every record where the nurse is the first element in the bookings array.

What I would like to add is a second view where all subsequent bookings can be seen, ie. records where the nurse is the nth booking for n > 0.

Is there a trivial way to do this? Or am I better off retrieving all documents first and then filtering on the actual document?

1 Answer 1

1

Just do both arguments. It's perfectly valid. So the query contains both the positive condition that looks at all elements and the $ne condition on only the first element:

db.bookings.find({ 
  "bookings.station": "Doctor", 
  "bookings.0.station": { "$ne": "Doctor" }
})

Returns, and:

db.bookings.find({
 "bookings.station": "Nurse", 
 "bookings.0.station": { "$ne": "Nurse" }
})

Does not. Which is the intent of course.

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

1 Comment

So simple and elegant, how did I not figure that out myself. Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.