提问者:小点点

MongoDB-查询的奇怪行为


我的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" : [ ]
}

我有这个代码来获取查询量:

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

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

在这种情况下,金额为2是错误的-它应该是1。Query对象中的查询如下所示:

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

如果我复制此查询并对mongoDB控制台进行如下查询:

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

我得到两个量,如果错误怎么办,因为一个文档在userIdBlackList中包含587f39910cc0ft092bdb10a-

使用此查询命令:

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

我得到了正确的结果,我现在真的很困惑。有人知道吗?也许问题是一次userIdBlackList带有引号(“userIdBlackList”),另一次没有。


共1个答案

匿名用户

我认为问题在于为"userIdBlackList"选择了无意的格式。对于您的所有搜索查询,您的字符串在"?? userIdBlackList"中被解释为非打印字符。当我将您的查询复制到shell时,我看到mongo透明的小方框。

这告诉我他们是一些编码问题。清除格式,看看这是否对你有帮助。

$ne$nin都应该可以!