`$和`查询中的SyntaxError

时间:2017-02-20 14:35:23

标签: mongodb

我正在尝试在mongodb中动态创建$and查询。但我收到错误SyntaxError: missing ; before statement @(shell)。是否无法动态创建$and查询?

db.firstcoll.find({
        $and: [{
                Thing: {
                    $in: stuff
                }
            },
            {
                Thing: {
                    $ne: "One three"
                }
            },
            {
                Category: "Second"
            }
        ]
    }).forEach(function(doc1) {
        var words = doc1.Thing.split(' ');
        var NewQuery;
        if (words.length == 1) {
            NewQuery = {
                $and: [{
                        Thing: {
                            $regex: words[0]
                        }
                    },
                    {
                        Category: doc1.Category
                    }
                ]
            }
        };
        if (words.length == 2) {
            NewQuery = {
                $and: [{
                    Thing: {
                        $regex: words[0]
                    }
                }, {
                    Thing: {
                        $regex: words[1]
                    }
                }, {
                    Category: doc1.Category
                }]
            }
        };

        db.secondcoll.find(
            NewQuery
        ).forEach(function(doc2) {
            print("stuff: ", doc2.stuff)

        })

    })

0 个答案:

没有答案