mongodb - How to reference each _id in a collection -
i have 2 collections on db.
at first have "tvs" collection have list of tvs, @ second have tvcomments holds comments made in each tv.
i update "tvs" collection new field shows count of comments each tv.
the "tv" field "_id" field "tvs" collection references commented tv.
i prove code bellow isn´s works
db.tvs.update( { $set: { numcomments: db.tvcomments.find( { tv: this._id } ).count() } }, { multi: true } )
default _id
keys in mongo mongo object ids. have convert this._id
objectid: objectid(this._id)
.
your command becomes:
db.tvs.update( { $set: { numcomments: db.tvcomments.find( { tv: objectid(this._id) } ).count() } }, { multi: true } )
objectid 12-byte bson type, constructed using:
- a 4-byte value representing seconds since unix epoch,
- a 3-byte machine identifier,
- a 2-byte process id, and
- a 3-byte counter, starting random value.
see more information in documentation.
Comments
Post a Comment