mongodb query on DBRef type

醉酒当歌 提交于 2020-01-05 08:36:22

问题


How do I turn this query in a valid mongodb Query in mongodb shell.

{ 'cars.owner.$ref' : 'users' } 

cars.owner is a DBRef here, but $ref is invalid

I get this error:

"$err" : "Positional operator does not match the query specifier."

My objective here is to figure out if there are any cars "owned" by different collections then users.


回答1:


MongoDB documentation (database-reference) says following:

MongoDB applications use one of two methods for relating documents:

  • Manual references where you save the _id field of one document in another document as a reference. Then your application can run a second query to return the related data. These references are simple and sufficient for most use cases.
  • DBRefs are references from one document to another using the value of the first document’s _id field, collection name, and, optionally, its database name. By including these names, DBRefs allow documents located in multiple collections to be more easily linked with documents from a single collection. To resolve DBRefs, your application must perform additional queries to return the referenced documents. Many drivers have helper methods that form the query for the DBRef automatically. The drivers 1 do not automatically resolve DBRefs into documents.

I don't know about any plans, but I have read that DBRefs should be deprecated(?) nowadays. However, you can use fetch() -method to load referenced documents. For example, I have formView collection which stores documents that contains (DBRef) references to documents in formContent collection. So, running:

var view = db.formView.findOne()
view.formContent
view.formContent.fetch()
view.formContent.fetch().code

...Will result in following output:

DBRef("formContent", ObjectId("56cb155ea826872b67373e76"))
{
    "_id" : ObjectId("56cb155ea826872b67373e76"),
    "code" : "test_content",
    "version" : 0
}
test_content



回答2:


You can query the DBRef in the Mongo shell, but you have to use the DBRef() function. The reference must include at least the $ref and $id. From the docs:

DBRef documents resemble the following document:

{ "$ref" : <value>, "$id" : <value>, "$db" : <value> }

When cars.owner is a reference to a document in the users collection, a query to find all cars where owner is a certain _id might look like (assuming both collections are in the same database):

db.cars.find({ "owner" : DBRef("users", ObjectId("<user _id value>")) })

The $ref and $id values cannot be queried directly. The DBRef is most useful in the case where there are multiple references to different collections in the same document. Using DBRef might be overkill when there is only one reference in the document, as others have mentioned.

If you need to reference different collections in your owners field, you are probably better served by using separate owner_collection and owner_id fields. The query to find all owners that are not users would then be a standard query:

db.cars.find({ owner_collection: { $ne: "users" } })



回答3:


<parameterName>: DBRef("CollectionName", "_id Value")

put in collection.Then Create a Bean having feild name as parameterName, and put @Reference annotation of morphia orm then do operations you want



来源:https://stackoverflow.com/questions/31753058/mongodb-query-on-dbref-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!