mongodb-如何获取包含特定DBRef对象的所有集合

时间:2019-02-17 15:38:17

标签: mongodb

您好,我有以下包含DBRef对象的产品集合,我的问题是如何查询类别C1或C2的所有产品集合,这是我的shema:

{ "_id" : ObjectId("5c27a71"), "name" : "HP", "price" : 837.10, "category" : DBRef("category", "C1"), "_class" : "com.entities.Product" }
{ "_id" : ObjectId("5c27a36"), "name" : "DELL", "price" : 489.10, "category" : DBRef("category", "C1"), "_class" : "com.entities.Product" }
{ "_id" : ObjectId("5c27a20"), "name" : "ASUS", "price" : 334.01, "category" : DBRef("category", "C1"), "_class" : "com.entities.Product" }
{ "_id" : ObjectId("5c27a78"), "name" : "LENEVO", "price" : 76.21, "category" : DBRef("category", "C1"), "_class" : "com.entities.Product" }
{ "_id" : ObjectId("5c27a74"), "name" : "THINKPAD", "price" : 500.84, "category" : DBRef("category", "C1"), "_class" : "com.entities.Product" }
{ "_id" : ObjectId("5c27a23"), "name" : "AX3D", "price" : 186.28, "category" : DBRef("category", "C2"), "_class" : "com.entities.Product" }
{ "_id" : ObjectId("5c27a12"), "name" : "BAF5", "price" : 614.58, "category" : DBRef("category", "C2"), "_class" : "com.entities.Product" }
{ "_id" : ObjectId("5c27a78"), "name" : "COMPAX MAX", "price" : 791.92, "category" : DBRef("category", "C2"), "_class" : "com.entities.Product" }
{ "_id" : ObjectId("5c27a54"), "name" : "GERO 55K", "price" : 576.28, "category" : DBRef("category", "C2"), "_class" : "com.entities.Product" }

这是类别文档的提示:

{ "_id" : "C1", "name" : "Ordinateurs", "products" : [ ], "_class" : "com.entities.Category" }
{ "_id" : "C2", "name" : "Imprimantes", "products" : [ ], "_class" : "com.entities.Category" }

您对如何查询所有产品的特定类别有任何想法。

预先感谢

1 个答案:

答案 0 :(得分:1)

您应该尝试执行此操作,如mongodb Docs

中所述
db.collectionsName.find(
   { category: { $in: [ DBRef("category", "C1"), DBRef("category", "C2") ] } }
)