Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Does MongoDB's $in clause guarantee order?

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 850
    Comment on it

    Hello All,

    In this blog we will discuss whether MongoDB's $in clause guarantee order or not.

    The $in operator selects the documents where the value of a field equals any value in the specified array. To specify an $in expression, use the following prototype:

    db.collection_name.find( { fieldName: { $in: [ keywords_to_find] } } )
    

    Below Code demonstrates how keytags collection values are saved in collection in natural order.

    Find Clause-

    > db.keytags.find().pretty()
    {
        "__v" : 0,
        "_id" : ObjectId("56499d341af7854313c2fb8a"),
        "additionalProperties" : {
            "wifiTagDescription" : "",
            "networkType" : "WEP",
            "password" : "Ankit11",
            "ssid" : "ankit.chettri%40evontech.com"
        },
        "businessId" : ObjectId("55c337720545813123acf09f"),
        "clientId" : ObjectId("55c33660cfc3bb56068b4567"),
        "createdAt" : ISODate("2015-11-16T09:09:08.923Z"),
        "creatorAccountId" : ObjectId("55c3366672a63c6a2246af3e"),
        "dataChecksum" : "3d794b6847d0e7b2fcc355462b2e1d95",
        "deleted" : "false",
        "domain" : "evontech.com",
        "donotIndex" : false,
        "isDomainCard" : true,
        "kiTAG" : "connect#evontech.com",
        "lastIndexedOn" : null,
        "manualUpdate" : true,
        "name" : "connect",
        "relatedKeywords" : "",
        "seo" : true,
        "status" : "ACTIVE",
        "title" : "TEST",
        "type" : "Wi-Fi",
        "updatedAt" : ISODate("2014-12-17T06:17:17.461Z")
    }
    {
        "__v" : 0,
        "_id" : ObjectId("55c9cbffcad924c90f3a3e7e"),
        "additionalProperties" : {
            "contentImage" : "",
            "description" : "%3Cp%3Ehello%20test%3C%2Fp%3E%0A",
            "talkback" : true
        },
        "businessId" : ObjectId("55c337720545813123acf09f"),
        "clientId" : ObjectId("55c33660cfc3bb56068b4567"),
        "createdAt" : ISODate("2015-08-11T10:18:39.293Z"),
        "creatorAccountId" : ObjectId("55c3366672a63c6a2246af3e"),
        "dataChecksum" : "ff5baf67be234fcbc789570a2009c4d4",
        "deleted" : "false",
        "domain" : "evontech.com",
        "donotIndex" : false,
        "isDomainCard" : true,
        "kiTAG" : "about#evontech.com",
        "lastIndexedOn" : null,
        "manualUpdate" : true,
        "name" : "about",
        "relatedKeywords" : "",
        "seo" : true,
        "status" : "ACTIVE",
        "title" : "Test",
        "type" : "Content",
        "updatedAt" : ISODate("2015-05-18T06:11:53.687Z")
    }
    {
        "__v" : 0,
        "_id" : ObjectId("564ac5c0171af45e0d72354a"),
        "additionalProperties" : {
            "wifiTagDescription" : "",
            "networkType" : "WEP",
            "password" : "Ankit11",
            "ssid" : "ankit.chettri%40evontech.com"
        },
        "businessId" : ObjectId("55c337720545813123acf09f"),
        "clientId" : ObjectId("55c33660cfc3bb56068b4567"),
        "createdAt" : ISODate("2015-11-17T06:14:24.546Z"),
        "creatorAccountId" : ObjectId("55c3366672a63c6a2246af3e"),
        "dataChecksum" : "d86a0604d8680e0fbc001ac756ea7698",
        "deleted" : "false",
        "domain" : "evontech.com",
        "donotIndex" : false,
        "isDomainCard" : false,
        "kiTAG" : "test#evontech.com",
        "lastIndexedOn" : null,
        "manualUpdate" : true,
        "name" : "test",
        "relatedKeywords" : "",
        "seo" : true,
        "status" : "ACTIVE",
        "title" : "test",
        "type" : "Wi-Fi",
        "updatedAt" : ISODate("2015-11-17T06:32:52.320Z")
    }
    {
        "__v" : 0,
        "_id" : ObjectId("564b21fd5efcf49c204b131f"),
        "additionalProperties" : {
            "urlImage" : "",
            "urlTagDescription" : "%3Cp%3Ethis%20is%20test%3C%2Fp%3E%0A",
            "url" : "http://www.test.com"
        },
        "address" : [ ],
        "businessId" : ObjectId("55c337720545813123acf09f"),
        "clientId" : ObjectId("55c33660cfc3bb56068b4567"),
        "createdAt" : ISODate("2015-11-17T12:47:57.870Z"),
        "creatorAccountId" : ObjectId("55c3366672a63c6a2246af3e"),
        "dataChecksum" : "345674fe5d42fc9e12f6d66ec4cb7f57",
        "deleted" : "564b21fd5efcf49c204b131f",
        "domain" : "evontech.com",
        "donotIndex" : false,
        "isDomainCard" : false,
        "kiTAG" : "testing#evontech.com",
        "lastIndexedOn" : null,
        "manualUpdate" : true,
        "name" : "testing",
        "relatedKeywords" : "",
        "seo" : true,
        "status" : "ACTIVE",
        "title" : "Testing",
        "type" : "URL",
        "updatedAt" : ISODate("2015-11-17T12:47:57.870Z")
    }
    {
        "additionalProperties" : {
            "urlImage" : "",
            "urlTagDescription" : "%3Cp%3ECheck%20One%3C%2Fp%3E%0A",
            "url" : "http://www.test.com"
        },
        "lastIndexedOn" : null,
        "creatorAccountId" : ObjectId("55c3366672a63c6a2246af3e"),
        "clientId" : ObjectId("55c33660cfc3bb56068b4567"),
        "businessId" : ObjectId("55c337720545813123acf09f"),
        "domain" : "evontech.com",
        "relatedKeywords" : "",
        "name" : "check one",
        "kiTAG" : "checkone#evontech.com",
        "type" : "URL",
        "title" : "Check%20One",
        "_id" : ObjectId("564c42fecf3fcc200ec1806a"),
        "status" : "ACTIVE",
        "isDomainCard" : false,
        "donotIndex" : false,
        "dataChecksum" : "8bc462e65c934f6478bf0f41a6f3e30a",
        "manualUpdate" : true,
        "updatedAt" : ISODate("2015-11-18T09:21:02.296Z"),
        "createdAt" : ISODate("2015-11-18T09:21:02.296Z"),
        "deleted" : "false",
        "address" : [ ],
        "seo" : true,
        "__v" : 0
    }
    > 
    

    Example-

    db.keytags.find( { "kiTAG": { $in: ["checkone#evontech.com","connect#evontech.com",] } } ).pretty()
    

    Output-

    {
        "__v" : 0,
        "_id" : ObjectId("56499d341af7854313c2fb8a"),
        "additionalProperties" : {
            "wifiTagDescription" : "",
            "networkType" : "WEP",
            "password" : "Ankit11",
            "ssid" : "ankit.chettri%40evontech.com"
        },
        "businessId" : ObjectId("55c337720545813123acf09f"),
        "clientId" : ObjectId("55c33660cfc3bb56068b4567"),
        "createdAt" : ISODate("2015-11-16T09:09:08.923Z"),
        "creatorAccountId" : ObjectId("55c3366672a63c6a2246af3e"),
        "dataChecksum" : "3d794b6847d0e7b2fcc355462b2e1d95",
        "deleted" : "false",
        "domain" : "evontech.com",
        "donotIndex" : false,
        "isDomainCard" : true,
        "kiTAG" : "connect#evontech.com",
        "lastIndexedOn" : null,
        "manualUpdate" : true,
        "name" : "connect",
        "relatedKeywords" : "",
        "seo" : true,
        "status" : "ACTIVE",
        "title" : "TEST",
        "type" : "Wi-Fi",
        "updatedAt" : ISODate("2014-12-17T06:17:17.461Z")
    }
    {
        "additionalProperties" : {
            "urlImage" : "",
            "urlTagDescription" : "%3Cp%3ECheck%20One%3C%2Fp%3E%0A",
            "url" : "http://www.test.com"
        },
        "lastIndexedOn" : null,
        "creatorAccountId" : ObjectId("55c3366672a63c6a2246af3e"),
        "clientId" : ObjectId("55c33660cfc3bb56068b4567"),
        "businessId" : ObjectId("55c337720545813123acf09f"),
        "domain" : "evontech.com",
        "relatedKeywords" : "",
        "name" : "check one",
        "kiTAG" : "checkone#evontech.com",
        "type" : "URL",
        "title" : "Check%20One",
        "_id" : ObjectId("564c42fecf3fcc200ec1806a"),
        "status" : "ACTIVE",
        "isDomainCard" : false,
        "donotIndex" : false,
        "dataChecksum" : "8bc462e65c934f6478bf0f41a6f3e30a",
        "manualUpdate" : true,
        "updatedAt" : ISODate("2015-11-18T09:21:02.296Z"),
        "createdAt" : ISODate("2015-11-18T09:21:02.296Z"),
        "deleted" : "false",
        "address" : [ ],
        "seo" : true,
        "__v" : 0
    }
    > 
    

    However as stated in above the order of the arguments in an array of an $in clause does not fetches the order of how the documents are retrieved as it follows the natural order in which documents are stored as shown in above find clause.

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: