Created
January 11, 2020 21:47
-
-
Save dhonig/afc330f96b3bdcd035ab3407c2f84f83 to your computer and use it in GitHub Desktop.
mongosearch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Requires official Node.js MongoDB Driver 3.0.0+ | |
var mongodb = require("mongodb"); | |
var client = mongodb.MongoClient; | |
var url = "mongodb://host:port/"; | |
client.connect(url, function (err, client) { | |
var db = client.db("test"); | |
var collection = db.collection("mains"); | |
var options = { | |
allowDiskUse: false | |
}; | |
var pipeline = [ | |
{ | |
"$searchBeta": { | |
"compound": { | |
"should": [ | |
{ | |
"search": { | |
"path": [ | |
"name", | |
"tags" | |
], | |
"query": "funny", | |
"fuzzy": { | |
"maxEdits": 1.0, | |
"prefixLength": 5.0 | |
}, | |
"score": { | |
"boost": { | |
"value": 5.0 | |
} | |
} | |
} | |
} | |
], | |
"filter": [ | |
{ | |
"term": { | |
"path": "products.department", | |
"query": "kids" | |
} | |
}, | |
{ | |
"term": { | |
"path": "products.style", | |
"query": "rbs" | |
} | |
} | |
], | |
"minimumShouldMatch": 1.0 | |
} | |
} | |
}, | |
{ | |
"$project": { | |
"score": { | |
"$meta": "searchScore" | |
}, | |
"_id": 1.0, | |
"name": 1.0 | |
} | |
} | |
]; | |
var cursor = collection.aggregate(pipeline, options); | |
cursor.forEach( | |
function(doc) { | |
console.log(doc); | |
}, | |
function(err) { | |
client.close(); | |
} | |
); | |
// Created with Studio 3T, the IDE for MongoDB - https://studio3t.com/ | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment