Skip to content

Instantly share code, notes, and snippets.

@dhonig
Created January 11, 2020 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhonig/afc330f96b3bdcd035ab3407c2f84f83 to your computer and use it in GitHub Desktop.
Save dhonig/afc330f96b3bdcd035ab3407c2f84f83 to your computer and use it in GitHub Desktop.
mongosearch
// 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