gpt4 book ai didi

mongodb - 如何查询带有点的属性?

转载 作者:可可西里 更新时间:2023-11-01 09:46:08 33 4
gpt4 key购买 nike

通常,MongoDB 不允许在属性名称中使用点。例如:

db.books.insert({"protagonist.name": "Guy Montog"});

因错误而失败:

uncaught exception: can't have . in field names [protagonist.name]

但是,我遇到了属性名称确实有点的情况。这可能发生在 system.profile 集合中。这是一个例子:

db.setProfilingLevel(2);
db.books.insert({protagonist: "Guy Montog", antecedents:
[{title: "The Fireman"}, {title: "Bright Phoenix"}]});
db.books.find({"antecedents.title": "The Fireman"})

现在如果我查看 system.profile 集合,我会看到以下记录:

> db.system.profile.findOne({op: "query"})
{
"ts" : ISODate("2012-10-14T00:05:49.896Z"),
"op" : "query",
"ns" : "wordswing.books",
"query" : {
"antecedents.title" : "The Fireman"
},
"nscanned" : 1,
"nreturned" : 1,
"responseLength" : 153,
"millis" : 0,
"client" : "127.0.0.1",
"user" : ""
}

假设我想查询查询“antecedents.title”的 system.profile 文件?这似乎是一个问题,因为属性名称中有一个点。

我尝试了以下所有方法:

db.system.profile.find({'query.antecedents.title': 'The Fireman'})
db.system.profile.find({'query.antecedents\.title': 'The Fireman'})
db.system.profile.find({"query.antecedents\.title": 'The Fireman'})

这些都不起作用。

想法?

这确实妨碍了我浏览相当大的 system.profile 集合的能力。

提前致谢。

更新

作为对评论的回应,我使用的版本是:

$ mongod --version
db version v2.0.6, pdfile version 4.5
Sun Oct 14 18:43:29 git version: e1c0cbc25863f6356aa4e31375add7bb49fb05bc

凯文

最佳答案

点分查询的配置文件条目似乎是一个错误.. key names在 MongoDB 中并不意味着包含 .(或前导 $)。

正如您所注意到的,这会导致您在尝试查询时出现问题,因为 dot notation用于指示嵌入对象。

MongoDB 2.2.0 中的一个有限解决方法似乎是使用聚合框架将查询对象匹配为嵌入式文档:

db.system.profile.aggregate({ $match: {'query': {"antecedents.title" : "The Fireman"}}})

我已经在 MongoDB 的问题跟踪器中报告了这个分析错误 SERVER-7349 .

关于mongodb - 如何查询带有点的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12878212/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com