假设我有以下文件
Article { Comment: embedMany }
Comment { Reply: embedMany }
Reply { email: string, ip: string }
Run Code Online (Sandbox Code Playgroud)
我想创建一个选择不同Reply.ip
位置的查询Reply.email = xxx
像这样的东西,只有它不起作用..
db.Article.find("Comment.Reply.email" : "xxx").distinct("Comment.Reply.ip")
Run Code Online (Sandbox Code Playgroud)
JSON导出:
{
"_id":{
"$oid":"4e71be36c6eed629c61cea2c"
},
"name":"test",
"Comment":[
{
"name":"comment test",
"Reply":[
{
"ip":"192.168.2.1",
"email":"yyy"
},
{
"ip":"127.0.0.1",
"email":"zzz"
}
]
},
{
"name":"comment 2 test",
"Reply":[
{
"ip":"128.168.1.1",
"email":"xxx"
},
{
"ip":"192.168.1.1",
"email":"xxx"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我跑:db.Article.distinct("Comment.Reply.ip",{"Comment.Reply.email" : "xxx"})
我期待:["128.168.1.1", "192.168.1.1"]
我明白了:["127.0.0.1", "128.168.1.1", "192.168.1.1", …
尝试使用 babel 变换,我编写了这段代码以VariableDeclaration
在我的之后插入一个path
export default function (babel) {
const { types: t } = babel;
return {
name: "ast-transform", // not required
visitor: {
VariableDeclaration (path) {
if (path.node.kind !== 'var') {
return;
}
const _id = t.Identifier('qwe');
const _init = t.numericLiteral(42);
console.log(_id, _init);
const _node = t.variableDeclaration('var', [
t.variableDeclarator(
t.identifier("uid"),
t.numericLiteral(42)
)
]);
path
.unshiftContainer(
'body',
_node
)
}
}
};
}
Run Code Online (Sandbox Code Playgroud)
得到以下错误,
未知:无法读取未定义的属性“body”
编辑:
如果我执行以下操作而不是unshiftContainer
有效,
const _program = path.findParent(p => p.isProgram());
_program.node.body.unshift(_node);
Run Code Online (Sandbox Code Playgroud)
为什么不起作用 …