I have two collections, both holding a very similar document. The document in each collection contains a key called 'users'. 'users' is a dictionary. Each key in the users dictionary is a 'user id', which points to some dictionary of data.
"users" : {
"58......abc":
{'num':1,type:'person'},
"58......def":
{'num':2,type:'person'},
"58......ghi":
{'num':3,type:'person'},
i have noticed that my userid 'keys', appear 'in order' in which they were inserted. for example when looking at the document of interest in collection1 and collection2 both appear to have the user_ids ordered with num=1 first and num=3 last.
i have heard talk that 'dictionaries are unordered' and you cannot count on their order staying the same.
However, my question is, if i query both collections through an end-point and iterate through both arrays with something like
var collection1_all_keys = Object.keys(d1.users);
var collection2_all_keys = Object.keys(d2.users);
for(var i = 0; i < collection1_all_keys.length; i++)
{
var collection1_user = d1.users[collection1_all_keys[i]];
var collection2_user = d2.users[collection2_all_keys[i]];
//compare differences
}
in this circumstance would i be safe to know that the collection1_user and collection2_user are the same user (via _id), with no additional sorting required after my query, on the server-side in nodejs?
if so, bonus if you can explain why the dictionaries keys in mongodb are ordered (like an array).
or alternatively, if the 'ordered keys' similarity of the users dictionary between documents is a coincidence, and is not guaranteed to persist, please explain.
//note: i am aware i could write a different for-loop, for example:
var collection1_all_keys = Object.keys(d1.users);
for(var i = 0; i < collection1_all_keys.length; i++)
{
var key = collection1_all_keys[i];
if(d1.users.hasOwnProperty(key) && d2.users.hasOwnProperty(key))
{
var collection1_user = d1.users[key];
var collection2_user = d2.users[key];
//compare differences
}
}
via user1709076
No comments:
Post a Comment