mongoose - MongoDB $pull efficiency -
let's assume following schema
var schema = new mongoose.schema({ data: { type: [number] } }); schema.index({ _id: 1, data: 1 }); var model = mongoose.model('test', schema);
how efficient $pull
operator when removing entries doc.data
?
model.update({ _id: someid }, { $pull: { data: { $lte: 123 } } }).exec();
will use index , run in o(log n + m) complexity, n number of elements in data , m number of removed elements? or have scan whole array?
and what's complexity of removing element after mongo finds it? o(1), o(log n) or o(n) has shift other items?
the collection's indexes used doc finding part of update, not update itself.
so built-in index on _id
used find document, $pull
update require whole data
array read , scanned against {$lte: 123}
query: o(n).
the index added on { _id: 1, data: 1 }
used if included data
in actual query as:
model.update({_id: someid, data: {$lte: 123}}, {$pull: {data: {$lte: 123}}}).exec();
but provide benefit in case data
contained no elements value <= 123 short-circuiting update doc no longer match criteria. still wouldn't used find data
elements $pull
itself.
Comments
Post a Comment