discontiguous array index keys

32 views
Skip to first unread message

tim.d...@gmail.com

unread,
Apr 18, 2016, 9:47:16 PM4/18/16
to mongodb-user
I just discovered that I have been inserting documents that contain arrays whose elements are indexed with odd numbers only. E.g., document looks like
{_id:1,a:[100,200,300]}
but array element keys are "1","3","5".  e.g., db.find({'a:5':300}) returns the document while db.find({'a:2':300}) does not.
This is possible because the C-driver takes a key value when you append array elements to a bson object you are building.  It is supposed to be "0", "1", "2".... but I have accidentally been using "1","3","5",... without any apparently harm, unless I try to refer to a specific array element with dot notation as in above examples.  Then it does remember what index key was used on insertion, though I am not sure how to see that in the database, or what happens after updates.

I have been adding new elements with {$push:{a:{$each:[400,500]}}} where the $each array is indexed with "1" and "3".  Does that mean I get index keys [1,3,5,1,3] or maybe [1,3,5,6,8] or [1,3,5,6,7] or [0,1,2,3,4]?

For the most part, I never refer to the indices, and did not even realize the database was saving them.  I load an array, and then use the "bson_iter_next" to walk through the elements.  I think all of my updates using $push:$each or "$addToSet:$each have been working correctly.  Assuming I never need to refer to a specific element with dot notation, is there any reason I need to repair these index keys for proper array management?



tim.d...@gmail.com

unread,
Apr 20, 2016, 11:48:32 AM4/20/16
to mongodb-user
If anyone else has made this error, it appears that:   db.collection.copyTo(new_collection) will set array element keys to "0","1","2",... in the new collection.

Asya Kamsky

unread,
Apr 21, 2016, 1:13:13 AM4/21/16
to mongod...@googlegroups.com
It looks like you ran into 
https://jira.mongodb.org/browse/CDRIVER-504

It's possible for it to cause a subtle access issue - glad you found a workaround to fixing them.  

Would you mind adding a comment with your description of the problem to the Jira ticket?   It'll help prioritize it knowing that more people have run into it. 

Asya
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/1e44f511-3c9d-4445-92d3-02fc71fe0a3b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Asya Kamsky
Lead Product Manager
MongoDB
Download MongoDB - mongodb.org/downloads
Free MongoDB Monitoring - cloud.mongodb.com
Free Online Education - university.mongodb.com
Get Involved - mongodb.org/community
We're Hiring! - https://www.mongodb.com/careers
Reply all
Reply to author
Forward
0 new messages