我现在有这些代码:
OneCollection.find({}, {fields: {'oneFiled.child1': 1}});
OneCollection.find({}, {fields: {'oneFiled.child2': 1}});
但我想给一个动态的儿童档名。
let childfield = "child1";
OneCollection.find({}, {fields: {'oneFiled.childfield': 1}}); // How to write this one?
如何动态提供已归档的名称?感谢
答案 0 :(得分:3)
使用 bracket notation 构建字段对象,如下所示:
var childfield = "child1",
options = { fields: {} };
options["field"]["oneField."+childfield] = 1;
OneCollection.find({}, options);
答案 1 :(得分:2)
喜欢这个吗?
C:\Users\achraf\AppData\Roaming\npm