简体   繁体   English

查询mongodb dbref内部字段

[英]Querying mongodb dbref inner field

I need to hide all the user related data whose isActive flag is set to false. 我需要隐藏其isActive标志设置为false的所有用户相关数据。 There are many collection in which I have used user collection as of type DBRef (around 14 collections) and each collection contains more than 10 million records. 有许多集合我使用了DBRef类型的用户集合(大约14个集合),每个集合包含超过1000万条记录。

Let me explain it more properly with help of example. 让我借助例子更恰当地解释一下。

Suppose I have two collections: 假设我有两个集合:

  1. User 用户
  2. Contact 联系

User collection contains following fields: 用户集包含以下字段:

  1. Firstname (String) 名字(字符串)
  2. Last Name (String) 姓氏(字符串)
  3. isActive (Boolean) isActive(布尔)

Contact collection contains following fields: 联系人集合包含以下字段:

  1. Contacter (User) Declared as of type DBref. Contacter(用户)声明为DBref类型。
  2. Contactee (User) Declared as of type DBRef. Contactee(User)声明为DBRef类型。
  3. ContactStatus (String) ContactStatus(String)

Now I want to fire a query which will fetch all the contacts whose ContactStatus = "Confirmed" && Contacter.isActive = true && Contactee.isActive = true 现在我想触发一个查询,它将获取ContactStatus = "Confirmed" && Contacter.isActive = true && Contactee.isActive = true所有联系人ContactStatus = "Confirmed" && Contacter.isActive = true && Contactee.isActive = true

In terms of mongodb, the query will be something like this: 就mongodb而言,查询将是这样的:

db.Contacts.find({"ContactStatus" : "Confirmed", "Contacter.isActive" : true, "Contactee.isActive" : true});

But when I run this query in mongo shell, it always returns a zero record. 但是当我在mongo shell中运行此查询时,它总是返回零记录。

So the question here is 1) Is it possible to fire a query on the DBRef's inner field ? 所以这里的问题是1)是否可以在DBRef的内部字段上触发查询? 2) If not, then how can I achieve that. 2)如果没有,那我该怎么做呢。

Note - At this stage, I am not able to modify my data model. 注意 - 在此阶段,我无法修改数据模型。 With the help of "in" query, I can achieve this. 在“in”查询的帮助下,我可以做到这一点。 But it will ultimately increase one round trip everywhere where I need to hide that user. 但它最终会在我需要隐藏该用户的地方增加一次往返。

Currently I am using mongodb-2.4.5 and Spring-Data-MongoDB-1.2.3 jar 目前我使用的是mongodb-2.4.5和Spring-Data-MongoDB-1.2.3 jar

So far my code is like this - 到目前为止我的代码是这样的 -

Criteria criteria = new Criteria();
criteria = criteria.where(Contact.CONTACT_REQUEST_STATUS).is(ContactRequestStatusEnum.ACCEPTED);
criteria = criteria.and(Contact.CONTACTER + "." + User.ACTIVE).is(Boolean.TRUE);
criteria = criteria.and(Contact.CONTACTEE + "." + User.ACTIVE).is(Boolean.TRUE);

Query q = new Query(criteria);
List<Contact> contacts = Contacts.find(q, Contact.class);

Yes, you can query on the DbRef fields, but not the way you are doing it. 是的,您可以查询DbRef字段,但不能查询您的方式。

DbRef is a small sub-documents which contains two fields: DbRef是一个小的子文档,包含两个字段:

$ref -the referenced collection $ref ref-引用的集合

$id - the _id value of a document in that referenced collection $id - 引用集合中文档的_id值

(actually there is a third field $db if the reference is to a different db) (实际上,如果引用的是另一个db,则会有第三个字段$db db)

So, using the shell you can only ask for contacter.$id (which returns the Object id in users collection) or $ref, but you can't query on something such as contract.isActive, as this is a field of the user, not the Ref, and the shell doesn't fetch the user. 所以,使用shell你只能要求contacter。$ id(返回用户集合中的Object id)或$ ref,但是你不能查询诸如contract.isActive之类的东西,因为这是用户的一个字段,而不是Ref,shell不会获取用户。

If you are using java driver, both Contacter and Contactee are represented as com.mongodb.DBRef which has a method fetch() to retrieve the DBObject (user) 如果您使用的是java驱动程序,则Contacter和Contactee都表示为com.mongodb.DBRef ,它有一个方法fetch()来检索DBObject (用户)

If using spring-data-mongodb, you might want to have class such as: 如果使用spring-data-mongodb,您可能希望拥有如下类:

class Contact {

@DBRef
User contacter; 

@DBRef
User contactee;

String contactStatus; 

}

Both user objects will be loaded for you 将为您加载两个用户对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM