【发布时间】:2018-03-04 18:08:34
【问题描述】:
有没有办法在 mongodb 3.4/3.6 中使用 jsonpath?示例如下所示,参考链接JSON path parent object, or equivalent MongoDB query:
var t1 = db.runCommand({aggregate : 'news_feeds',
pipeline:[],
cursor : {batchSize : 10}});
t1.cursor.firstBatch.forEach(function(doc)
{
var json = tojson(doc);
var json1="$.feeds.reviews[*].name"
var result = jsonPath(json, json1);
print(result);
})
这个查询给出了一个jsonPath没有定义的错误,但是这个jsonpath在javascript中有
请帮忙
问候
克里斯
【问题讨论】:
-
该错误是因为 MongoDB shell 无法将
jsonPath识别为函数。 MongoDB 目前不支持 jsonPath。 -
在 mongodb 中是否有另一种使用 jsonpath 的方法?
-
即使我使用 npm 安装了 json-path,并尝试执行,它说 require 未定义,请帮助 var jpath = require('json-path'); var t1 = db.runCommand({aggregate: 'news_feeds', pipeline:[], cursor: {batchSize: 10}}); t1.cursor.firstBatch.forEach(function(doc) { var json = tojson(doc); var res = jpath.resolve(json, "$.feeds.reviews[*].name") print(res); })
-
npm和 MongoDB shell 是两个不同的东西。npm用于节点。两者之间唯一的相似之处是都使用 Javascript 作为语言。我不熟悉 jsonPath 是如何实现的,但是可能有一种方法可以在mongoshell 中执行 jsonPath 包,但 MongoDB 本身从不支持使用 jsonPath。