문제

I'm trying to filter a MongoDB collection with a .find() query and run a text search on the results to lower the cost of the query but I can't seem to be able to chain the commands.

Here's what I've tried (that doesn't work):

db.jobs.find({
    "salary.max": {
        $gte: 50000,
        $lte: 120000
    }
}).runCommand("text", {
    search: "metal"
})

I've also tried the query in the reverse order, which defeats the purpose and doesn't work either.

Is there a way to chain a .runCommand() to a .find() in MongoDB?

도움이 되었습니까?

해결책

the .find function returns a DBCursor which hasn't got a a .runCommand-function. So this obviously doesn't work.

But what does work is using your find-query in the text database command. As you can read in the documentation for text searching, you can pass a filteras an optional parameter to the text command. These filter documents work exactly like those you pass to find.

db.jobs.runCommand( "text", { 
    search: "metal",
    filter: { 
        "salary.max": {
            $gte: 50000,
            $lte: 120000
        }
    }
} );
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top