0

Is there a way to determine the index of a retrieved item in an array in MongoDB?


I have an array of object ids inside some document.

{
    ids: [id1, id2, id3, ...]
}

I search the array for id3, and if it's found, I also want the position of the object added to the result. In this case, it's so I can insert more objects immediately after it in the array.

If I can't do this, how would I go about creating an ordered list of objects for which I can retrieve and easily modify the position of elements in the ordered list (since an insert operation on the list will result in modifying the position of all elements after the insert)?


answer:

Obtaining the positional index is NOT supported in mongodb. I will open a new question then about redesigning my application to deal with this limitation. Thanks everyone!

1
  • There isn't an operator to return the position in the array that matched. I think the most helpful thing would be a more complete description of what you're trying to do and why so that we can come up with the most viable options. Commented Sep 10, 2014 at 18:28

1 Answer 1

1

The way I see it, you have two choices:

  • Retrieve the whole array, mutate it however you wish in your language of choice, and update the array in the db with your new array.

  • Come up with a data structure that supports whatever it is you are ultimately trying to do.

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

Comments

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.