Seiyria
Seiyria

Reputation: 2132

mongodb / node - ignore certain object properties

Is there a way to tell MongoClient that there are some properties I don't want to store? For example, I have some properties that are cyclic dependencies which fail to serialize - this causes a few problems. I'd rather not have to set them to null before I save them every time, then re-instate those variables when the insert has finished.

Upvotes: 0

Views: 411

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311865

One way to do this is with a little help from the omit method of the underscore (or lodash) library. That will cleanly create a copy of your object without the problematic properties.

var objectToInsert = _.omit(fullObject, 'badField1', 'badField2');
collection.insert(objectToInsert, callback);

Another way to go is to use Mongoose which lets you define schemas for your collections so that only those fields in the schema get included when saved.

Upvotes: 1

Related Questions