简体   繁体   English

MongoDB-查询的奇怪行为

[英]MongoDB - strange behaviour of query

I have this two documents in my mongoDB database: 我的mongoDB数据库中有这两个文档:

db.DocumentFile.find().pretty()
{
"_id" : ObjectId("587f39910cc0fec092bdb10c"),
"_class" : "com.smartinnotec.legalprojectmanagement.dao.domain.DocumentFile",
"fileName" : "DocumentFile1",
"ending" : "jpg",
"projectId" : "587f39910cc0fec092bdb10b",
"active" : true,
"userIdBlackList" : [
    "587f39910cc0fec092bdb10a"
]
}
{
"_id" : ObjectId("587f39910cc0fec092bdb10d"),
"_class" : "com.smartinnotec.legalprojectmanagement.dao.domain.DocumentFile",
"fileName" : "DocumentFile2",
"ending" : "jpg",
"projectId" : "587f39910cc0fec092bdb10b",
"active" : true,
"userIdBlackList" : [ ]
}

I have this code in order to get amount of query: 我有此代码,以获取查询量:

final Query query = new Query();
query.addCriteria(Criteria.where("‌​userIdBlackList").nin(userId));

final Long amount = mongoTemplate.count(query, DocumentFile.class);
return amount.intValue();

The amount is 2 in this case what is wrong - it should be 1. The query in Query object looks like this: 在这种情况下,数量为2错了,应该为1。Query对象中的查询如下所示:

Query: { "‌​userIdBlackList" : { "$nin" : [ "587f39910cc0fec092bdb10a"]}}

If I copy this query and made a query for the mongodb console like this: 如果我复制此查询并对mongodb控制台进行查询,如下所示:

db.DocumentFile.find({ "‌​userIdBlackList" : { "$nin" : [ "587f39910cc0fec092bdb10a"]}}).pretty()

I get an amount of two, what if wrong because one document includes 587f39910cc0fec092bdb10a in userIdBlackList -> it should be one. 我得到两个,如果出现错误怎么办,因为一个文档在userIdBlackList中包含587f39910cc0fec092bdb10a- >它应该是一个。

With this query command: 使用此查询命令:

db.DocumentFile.find({userIdBlackList: { "$nin": ["587f39910cc0fec092bdb10a"] } }).pretty();

I get the right result, I am really confused at the moment. 我得到正确的结果,此刻我真的很困惑。 Does anyone have any idea? 有人有什么主意吗? Maybe the problem ist that one time userIdBlackList is with quotation mark ("userIdBlackList") and the other time it isn't. 也许问题在于,一次userIdBlackList带有引号(“ userIdBlackList”),而另一次则没有。

I think the problem is with the unintentional formatting picked up for "userIdBlackList" . 我认为问题出在"userIdBlackList"的意外格式化上。 Your string is interpreted with non printing characters in the "??userIdBlackList" for all your search queries. 您的所有搜索查询的字符串都将以非打印字符的形式显示在"??userIdBlackList"中。 I see little transparent square boxes when I copy your queries to mongo shell. 将您的查询复制到mongo shell时,我看不到透明的小方框。

That tells me their is some encoding issue. 这告诉我他们是一些编码问题。 Clear that formatting and see if that helps you. 清除该格式,看看是否有帮助。

Both $ne and $nin should work! $ne$nin都应该起作用!

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

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