MongoDB

Advanced Serch Examples - Native MongoDB driver - $or , $regex , $elemMatch

1.

Document

{
    "_id" : ObjectId("55c23b8b8b70fe4e148fea45"),
    "cid" : 14,
    "pageURL" : "https://www.youtube.com/upload",
    "crawlDateTime" : "Wed Aug 05 2015 18:36:27 GMT+0200 (CEST)",
    "category" : 1,
    "subcategory" : 1,
    "extract" : {
        "title" : [
            "text",
            "title",
            "YouTube"
        ],
        "image" : [
            "src",
            "img",
            "//ssl.gstatic.com/images/icons/ui/common/universal_language_settings-21.png"
        ],
        "body_text" : [
            "text",
            "body div",
            "One account. All of Google.\n Sign in to continue to YouTube We'll only personalize this page when you're signing in from a familiar device or location. Learn more Sign in with a different account Create account One Google Account for everything Google\n\n About Google Privacy Terms Help One account. All of Google.\n Sign in to continue to YouTube We'll only personalize this page when you're signing in from a familiar device or location. Learn more Sign in with a different account Create account One Google Account for everything Google\n\n One account. All of Google.\n Sign in to continue to YouTube We'll only personalize this page when you're signing in from a familiar device or location. Learn more Sign in with a different account Create account One Google Account for everything Google\n\n We'll only personalize this page when you're signing in from a familiar device or location. Learn more We'll only personalize this page when you're signing in from a familiar device or location. Learn more Sign in with a different account Create account One Google Account for everything Google\n\n Sign in with a different account Create account One Google Account for everything Google\n\n Sign in with a different account Create account One Google Account for everything Google\n\n About Google Privacy Terms Help About Google Privacy Terms Help"
        ]
    }
}

 

queryDB = {
      $or: [
        {"pageURL": {$regex: reg}},
        {"extract.title": {$elemMatch: {$regex: reg}}},
        {"extract.body_text": {$elemMatch: {$regex: reg}}}
      ]
    };

 

db.collection(currentColl).find(queryDB).sort({cid: 1}).skip(pagination_obj.skipNum).limit(pagination_obj.perPage).toArray(function (err, moContentDocs_arr) {
          if (err) { logg.byWinston('error', __filename + ':67 ' + err); }

          cb_list(res, currentColl, contentColls, moContentDocs_arr, pagination_obj);
});