Cursor.oberveChange gets multiple calls to changed() for a single update


#1

I am using cursor.oberveChange() to observe what is going on in a Collection.

When doing MyCollection.update(id, { $set: { field1: value1, field2: value2 } }), I end up with two calls to the changed() callback: one for field1, another one for field2.

Is it the expected behavior? If it is, is there a way I can know when fields are set together?


#2

I believe that should only be called once per document changed, unless I’m mistaken.

Does your case sound like this? If so, looks like that should be fixed if you’re using the most current release.

Also, per the docs, observeChanges returns only the changed properties, whereas observe returns the entire old and new document with all its fields. So observeChanges should be what you’re looking for to see what specific fields change.

Any chance you could paste the snippets including your observe code and update code?


#3

Here is the update code:

Subsecs.update(
    selector, 
    { $set: {	 title: title, lock: { contrId: null, serverTime: new Date(0) } }
);

And the observeChange code:

		Subsecs.find({ projectId: projectId }).observeChanges(			{
    			added: function(id, subsec)	 { 
        manageLockAndRole(self, id, subsec); 	
        subsec._id = id;
        self.localSubsecs.insert(subsec); 
    },
    			changed: function(id, fields) 				{
    				    manageLockAndRole(self, id, fields);
        self.localSubsecs.update(id, { $set: fields });
    				},
    			removed: function(id) {
        self.localSubsecs.remove(id); 
    }
			});

One update triggers two calls to the changed() callback, the first one for lock, the second one for title.