简体   繁体   English

MongoDB:对过滤字段使用值1

[英]MongoDB : Using the value 1 for Filter Fields

I am working on a existing code in which they are using Mongo DB with Java EE. 我正在研究一个现有的代码,他们使用Mongo DB和Java EE。

Under which in some case for filteration purposes , they are using the hard coded value as 1 . 在某些情况下,为了过滤目的,他们使用硬编码值为1。

BasicDBObject query = new BasicDBObject(SEC_TYPE, "A");

BasicDBObject Fields = new BasicDBObject("date", 1);
returnFields.put("unisymbol", 1);
returnFields.put("symbol", 1);


DBCursor cursor = coll.find(query, Fields);

For example if you see the above query the Fields such as date , unisymbol and symbol are hardcoded to 1 . 例如,如果您看到上面的查询,则将日期,unisymbol和符号等字段硬编码为1。

Please let me know what does , putting the value "1" aganist the filed mean exactly ?? 请让我知道是什么做的,把值“1”aganist归结为准确的意思?

It means, "retrieve value only for this field". 这意味着,“仅为此字段检索值”。 So, this query: 所以,这个查询:

db.users.find({}, {firstname: 1, lastname: 1})

will get only two fields, first name and last name. 将只获得两个字段,名字和姓氏。 It won't get email, address and other fields. 它不会收到电子邮件,地址和其他字段。

It is used for projection: http://docs.mongodb.org/manual/core/read-operations/#result-projections whereby the fields of the document are being filtered out. 它用于投影: http//docs.mongodb.org/manual/core/read-operations/#result-projections ,用于过滤文档的字段。 In this case 1 against that field means to include it and 0 mens to omit it. 在这种情况下,针对该字段的1表示包含它而0表示省略它。

So the code above includes date , unisymbol , symbol and _id . 所以上面的代码包括dateunisymbolsymbol_id It includes the _id since that has to be explicitly removed from the result via: 它包含_id因为必须通过以下方式从结果中明确删除:

returnFields.put("_id", 0);

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

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