ronag
ronag

Reputation: 51255

How fine grained should my model be?

How fine grained should the data model using meteor be?

e.g.

Let's say I have a XmlDocument model which contains several XmlNodes.

Should I just simply create a new Meteor.Collection("Documents") and doing updates on a whole document basis, e.g.

Documents = new Meteor.Collection("documents");

Documents.insert(new XmlDocument());

var doc = Documents.findOne().fetch();

doc.nodes.push(new XmlNode());

Documents.update(doc);

Or should I split the nodes into a separate collection where every item has a an id to it's owning document (i.e. as it would have been with .NET Entity Framework)?

I'm having a bit of a hard time figuring this out since I am unsure how fine grained the Meteor implementation is, i.e. when doing update does it perform a diff with the current version and only perform the minimal amount of work against the server, or will it just send the entire data as a single transaction?

Upvotes: 0

Views: 62

Answers (2)

jindrichb
jindrichb

Reputation: 96

or will it just send the entire data as a single transaction?

Unfortunatelly yes, on any document change it will send whole document, down to client. So I think, it is not wise, build db structure this way - mean one big document in one collection (instead of several collections). How DDP work and what it will sending you can see if you install and enable Meteor DDP Analyzer.

Upvotes: 0

Łukasz Gozda Gandecki
Łukasz Gozda Gandecki

Reputation: 364

Instead of recreating the whole document everytime, use addToSet and pull into the array in your document.

var _newDocId = Documents.insert(new XmlDocument());
var _newNode = new XmlNode();
Documents.update({_id: _newDocId}, {$addToSet: {nodes: _newNode}});

It's a little tricky to update documents in arrays, so you might want to make sure this is what you want to do, instead of using separate collections but, you can do things like

var _document = Documents.findOne({_id: _documentContainingNode});    
var _nodeToUpdateIndex = _.indexOf(_.pluck(_document.nodes, '_id'), _nodeId);
var _modifier = {$set: {}};
_modifier.$set["nodes." + _nodeToUpdateIndex + ".description"] = "new description";

Documents.update({_id: _document._id}, _modifier);

Updating objects inside arrays is a downside of having big, nested objects. Downside of having them separated is that you have to make sure you publish them together.. And, obviously, you lose the atomicity of operations. If you want to make an update to both nodes and document at the same time. It's more of your decision than Meteor "implementation"

Upvotes: 1

Related Questions