These are chat archives for LearnBoost/mongoose

18th
Mar 2015
Richard Brookfield
@dvideby0
Mar 18 2015 16:17
has anyone else noticed that if you use addToSet on an empty array, the first set of objects inserted are reversed? Any operation after will adhere to the order in which you specify. here is some code that we were using to test and it consistently works. Just wondering why we have to reverse the initial inserts.
twitterFriends.findOne({screen_name: message.body.screen_name}, function(err, record){
        if(err){
          console.log(err);
          message.nack();
        }

        if(!record) {
          record = twitterFriends(
            {
              screen_name: message.body.screen_name,
              friends: []
            }
          );
          record.save(function(err) {
            if (err) {
              console.log('error: %j', err);
              message.nack();
            }
            console.log('create ' + record);
            data.forEach(function(friend){
              record.friends.addToSet({screen_name: friend.screen_name, name: friend.name, id: friend.id});
              record.save();
            });
            message.ack();
          });
        } else {
          console.log('update ' + record);
          data.forEach(function(friend){
            record.friends.addToSet({id: friend.id, name: friend.name, screen_name: friend.screen_name});
            record.save();
          });
          message.ack();
        }
      });
Valeri Karpov
@vkarpov15
Mar 18 2015 18:52
So $addToSet does not guarantee any particular ordering: http://docs.mongodb.org/manual/reference/operator/update/addToSet/#behavior
on the mongodb server end that is, that's not a mongoose restriction
although I suspect this may be a case of mongoose reversing keys. Thanks for your issue on Github, I'll look into it
Richard Brookfield
@dvideby0
Mar 18 2015 18:55
Does that not just refer to the order within the array? not the item in the array itself? because this behavior is 100% consistent. Its always reversed the 1st time and then as expected any time after. It never deviates from that. $addToSet only ensures that there are no duplicate items added to the set and does not affect existing duplicate elements. this seems to refer to the order in array
otherwise how would it be able to remove duplicates?
if items are objects
Valeri Karpov
@vkarpov15
Mar 18 2015 20:42
ah ok I misunderstood your question. It's the actual sub-document itself that's backwards, not the elements of the array. That's different, thanks for clarifying. I don't know off the top of my head but I'll look into it.
Richard Brookfield
@dvideby0
Mar 18 2015 20:42
Thank You. I reported a bug if you want to track there.. LearnBoost/mongoose#2771