mongoose tips and hacks

How to query an embedded document can be found on SO also.

  #with a premise that you sent an email and zip
      email :, 
      '' : #this is the "thing"
  }, function(err, docs){
    #more code with documents

Query with a "non-equivalent", "and" operations

#find if email is taken|used
     {_id: {$ne : user._id}}]
  function(err, docs){
        #do something about it.

How to use MongoDB as a Message Queue - hint :use Capped Collections

    #source mongoose documentation
    Schema({..}, { capped: { size: 1024, max: 1000, autoIndexId: true } });
    #Query and wait for new data.
    var stream = Schema.find(filter).tailable().stream(); 
    stream.on('data', fn(data){/**do something with data*/});

How to add/update array sub-documents

#update considering all object values
Group.update({_id: 123}, {$addToSet: {users: usrid}},{upsert:false}, function(error, grp){});

#update considering one or more conditions
var user = {uid: usrid};  
var selector = {name:'admin','users.uid':{$ne:user.uid}};  
Group.update(selector,{$push:{users: user}},  
function(err, numAffected) {});  


Show Comments