MongoDB
Basic MongoDB Search Queries
db.collection(collName)
.find(queryDB)
.sort({cid: -1}) // sort descending
.skip(8) // start from 8th document
.limit(10) //10 docs will be shown
.toArray(function (err, moContent_arr) {
//some code here
});
1. Regex Find
var regx = new RegExp(q, 'ig'); //creating regular expression
queryDB = {
"extract.description": {"$regex": regx}
};
2. Text Find
a) create text index: > db.content.createIndex({"extract.title":"text", "extract.description":"text"},{name:"tekstInd"})
b) define search query
queryDB = {
$text: { $search: q },
category: category
};
3. $elemMatch - find doc which array field contains value
{
"_id" : "pll::1",
"type" : "playlist",
"devices" : [
"dev::df8c25f3-89a8-3126-a3d2-74ce17b34482",
"dev::34e874d7-9d1f-31e7-af40-6abbb5258692",
"dev::00e80431-5f58-30b8-bbd3-e4ad275ea0fe",
"dev::73386716-119f-3f99-b016-89228f9a105d",
"dev::34e874d7-9d1f-31e7-af40-6abbb5258692"
]}
db.playlists.find({devices: {$elemMatch: {$eq: "dev::df8c25f3-89a8-3126-a3d2-74ce17b34482"}}})