gpt4 book ai didi

node.js - 从 mongodb/nodejs 查询嵌套对象数组

转载 作者:行者123 更新时间:2023-12-02 20:05:11 24 4
gpt4 key购买 nike

我试图弄清楚如何查询 Components 对象内的嵌套对象。数据是从解析的 json 文件插入的。

查询

var query = {}
cursor = db.collection("workflows").find(query).toArray(function(err, result) {
if (err) throw err;

console.log(result);
db.close();
});

当我运行上面的查询时返回此数据:

At this point i'm just trying to get it to filter in some manner. I've tried Name:'Test WF' and other variations of that but still can't get a filtered response.

[ { _id: 5c77040838f9d322b89bbd82,
texto:
{ _id: 12,
LocalCachePath: 'Z:\\Test\\Cache',
SharedCachePath: [],
Name: 'Test WF',
Desc: 'I\'m testing',
Components: [Array] } },
{ _id: 5c7704164413692978a9dd1a,
texto:
{ _id: 'Workflow-2019.02.22-23.21.15-MKRU',
LocalCachePath: 'Z:\\MAITest\\Cache',
SharedCachePath: [],
Name: 'Test WF',
Desc: 'I\'m testing',
Components: [Array] } },
{ _id: 5c77046335b012379c99951b,
texto:
{ _id: '154',
LocalCachePath: 'Z:\\Test\\Cache',
SharedCachePath: [],
Name: 'Test WF',
Desc: 'I\'m testing',
Components: [Array] } },
{ _id: 5c7704787bde6f36543d1016,
texto:
{ _id: 'Workflow-2019.02.22-23.21.15-MKRU',
LocalCachePath: 'Z:\\Test\\Cache',
SharedCachePath: [],
Name: 'Test WF',
Desc: 'I\'m testing',
Components: [Array] } } ]

任何见解都会有所帮助,我一次又一次地跌跌撞撞地完成了这一步。

这是另一个查询,它给了我结果,但我想我的问题是将我的结果解析为变量。

var query = {'texto.Components.0.Name' : {$gt: ''}}
// var query = {'testo.Name' : {$gt: ''} }
cursor = db.collection("workflows").find(query).toArray(function(err, result) {
if (err) throw err;

最佳答案

使用点表示法(例如texto.Name)从嵌套对象中查询和检索字段,例如:

var query = {'texto.Name': 'Test WF'}

关于node.js - 从 mongodb/nodejs 查询嵌套对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54915316/

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