dbref

How to query mongodb with DBRef

你离开我真会死。 提交于 2019-11-27 06:33:18
suppose I have the following datastructure: var user = {_id: 'foo', age: 35}; var post = {_id: '...', author: {$ref: user, $id: 'foo'},...}; How can I query all posts which references user[foo]? I tried the following but not work: db.post.find('author._id': 'foo'); var u = db.user.find({_id: 'foo'}); db.post.find('author': u); neither can I find the answer from the official document and google! Anyone has any idea? Got it: db.post.find({'author.$id': 'foo'}) You can use the .$id reference but it will ignore any indexes on those fields. I would suggest ignoring that method unless you are

MongoDB - is DBREF necessary?

做~自己de王妃 提交于 2019-11-26 13:58:10
问题 Using the DBREF datatype in MongoDB, a document may look like as shown below. But having the $ref field in every row feels redundant as every row obviously points to the users collection. Is there a way to reference other documents without having the somewhat redundant $ref -field? { $id: {$oid : "4f4603820e25f4c515000001"}, title: "User group", users: [ {_id: {$ref: "users", $id: { $oid: "4f44af6a024342300e000002"}}, isAdmin: true } ] ], 回答1: Dbref in my opinion should be avoided when work

How to query mongodb with DBRef

∥☆過路亽.° 提交于 2019-11-26 12:04:02
问题 suppose I have the following datastructure: var user = {_id: \'foo\', age: 35}; var post = {_id: \'...\', author: {$ref: user, $id: \'foo\'},...}; How can I query all posts which references user[foo]? I tried the following but not work: db.post.find(\'author._id\': \'foo\'); var u = db.user.find({_id: \'foo\'}); db.post.find(\'author\': u); neither can I find the answer from the official document and google! Anyone has any idea? 回答1: Got it: db.post.find({'author.$id': 'foo'}) 回答2: You can