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
    • 873
    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:

    1. 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-

    1. > db.keytags.find().pretty()
    2. {
    3. "__v" : 0,
    4. "_id" : ObjectId("56499d341af7854313c2fb8a"),
    5. "additionalProperties" : {
    6. "wifiTagDescription" : "",
    7. "networkType" : "WEP",
    8. "password" : "Ankit11",
    9. "ssid" : "ankit.chettri%40evontech.com"
    10. },
    11. "businessId" : ObjectId("55c337720545813123acf09f"),
    12. "clientId" : ObjectId("55c33660cfc3bb56068b4567"),
    13. "createdAt" : ISODate("2015-11-16T09:09:08.923Z"),
    14. "creatorAccountId" : ObjectId("55c3366672a63c6a2246af3e"),
    15. "dataChecksum" : "3d794b6847d0e7b2fcc355462b2e1d95",
    16. "deleted" : "false",
    17. "domain" : "evontech.com",
    18. "donotIndex" : false,
    19. "isDomainCard" : true,
    20. "kiTAG" : "connect#evontech.com",
    21. "lastIndexedOn" : null,
    22. "manualUpdate" : true,
    23. "name" : "connect",
    24. "relatedKeywords" : "",
    25. "seo" : true,
    26. "status" : "ACTIVE",
    27. "title" : "TEST",
    28. "type" : "Wi-Fi",
    29. "updatedAt" : ISODate("2014-12-17T06:17:17.461Z")
    30. }
    31. {
    32. "__v" : 0,
    33. "_id" : ObjectId("55c9cbffcad924c90f3a3e7e"),
    34. "additionalProperties" : {
    35. "contentImage" : "",
    36. "description" : "%3Cp%3Ehello%20test%3C%2Fp%3E%0A",
    37. "talkback" : true
    38. },
    39. "businessId" : ObjectId("55c337720545813123acf09f"),
    40. "clientId" : ObjectId("55c33660cfc3bb56068b4567"),
    41. "createdAt" : ISODate("2015-08-11T10:18:39.293Z"),
    42. "creatorAccountId" : ObjectId("55c3366672a63c6a2246af3e"),
    43. "dataChecksum" : "ff5baf67be234fcbc789570a2009c4d4",
    44. "deleted" : "false",
    45. "domain" : "evontech.com",
    46. "donotIndex" : false,
    47. "isDomainCard" : true,
    48. "kiTAG" : "about#evontech.com",
    49. "lastIndexedOn" : null,
    50. "manualUpdate" : true,
    51. "name" : "about",
    52. "relatedKeywords" : "",
    53. "seo" : true,
    54. "status" : "ACTIVE",
    55. "title" : "Test",
    56. "type" : "Content",
    57. "updatedAt" : ISODate("2015-05-18T06:11:53.687Z")
    58. }
    59. {
    60. "__v" : 0,
    61. "_id" : ObjectId("564ac5c0171af45e0d72354a"),
    62. "additionalProperties" : {
    63. "wifiTagDescription" : "",
    64. "networkType" : "WEP",
    65. "password" : "Ankit11",
    66. "ssid" : "ankit.chettri%40evontech.com"
    67. },
    68. "businessId" : ObjectId("55c337720545813123acf09f"),
    69. "clientId" : ObjectId("55c33660cfc3bb56068b4567"),
    70. "createdAt" : ISODate("2015-11-17T06:14:24.546Z"),
    71. "creatorAccountId" : ObjectId("55c3366672a63c6a2246af3e"),
    72. "dataChecksum" : "d86a0604d8680e0fbc001ac756ea7698",
    73. "deleted" : "false",
    74. "domain" : "evontech.com",
    75. "donotIndex" : false,
    76. "isDomainCard" : false,
    77. "kiTAG" : "test#evontech.com",
    78. "lastIndexedOn" : null,
    79. "manualUpdate" : true,
    80. "name" : "test",
    81. "relatedKeywords" : "",
    82. "seo" : true,
    83. "status" : "ACTIVE",
    84. "title" : "test",
    85. "type" : "Wi-Fi",
    86. "updatedAt" : ISODate("2015-11-17T06:32:52.320Z")
    87. }
    88. {
    89. "__v" : 0,
    90. "_id" : ObjectId("564b21fd5efcf49c204b131f"),
    91. "additionalProperties" : {
    92. "urlImage" : "",
    93. "urlTagDescription" : "%3Cp%3Ethis%20is%20test%3C%2Fp%3E%0A",
    94. "url" : "http://www.test.com"
    95. },
    96. "address" : [ ],
    97. "businessId" : ObjectId("55c337720545813123acf09f"),
    98. "clientId" : ObjectId("55c33660cfc3bb56068b4567"),
    99. "createdAt" : ISODate("2015-11-17T12:47:57.870Z"),
    100. "creatorAccountId" : ObjectId("55c3366672a63c6a2246af3e"),
    101. "dataChecksum" : "345674fe5d42fc9e12f6d66ec4cb7f57",
    102. "deleted" : "564b21fd5efcf49c204b131f",
    103. "domain" : "evontech.com",
    104. "donotIndex" : false,
    105. "isDomainCard" : false,
    106. "kiTAG" : "testing#evontech.com",
    107. "lastIndexedOn" : null,
    108. "manualUpdate" : true,
    109. "name" : "testing",
    110. "relatedKeywords" : "",
    111. "seo" : true,
    112. "status" : "ACTIVE",
    113. "title" : "Testing",
    114. "type" : "URL",
    115. "updatedAt" : ISODate("2015-11-17T12:47:57.870Z")
    116. }
    117. {
    118. "additionalProperties" : {
    119. "urlImage" : "",
    120. "urlTagDescription" : "%3Cp%3ECheck%20One%3C%2Fp%3E%0A",
    121. "url" : "http://www.test.com"
    122. },
    123. "lastIndexedOn" : null,
    124. "creatorAccountId" : ObjectId("55c3366672a63c6a2246af3e"),
    125. "clientId" : ObjectId("55c33660cfc3bb56068b4567"),
    126. "businessId" : ObjectId("55c337720545813123acf09f"),
    127. "domain" : "evontech.com",
    128. "relatedKeywords" : "",
    129. "name" : "check one",
    130. "kiTAG" : "checkone#evontech.com",
    131. "type" : "URL",
    132. "title" : "Check%20One",
    133. "_id" : ObjectId("564c42fecf3fcc200ec1806a"),
    134. "status" : "ACTIVE",
    135. "isDomainCard" : false,
    136. "donotIndex" : false,
    137. "dataChecksum" : "8bc462e65c934f6478bf0f41a6f3e30a",
    138. "manualUpdate" : true,
    139. "updatedAt" : ISODate("2015-11-18T09:21:02.296Z"),
    140. "createdAt" : ISODate("2015-11-18T09:21:02.296Z"),
    141. "deleted" : "false",
    142. "address" : [ ],
    143. "seo" : true,
    144. "__v" : 0
    145. }
    146. >

    Example-

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

    Output-

    1. {
    2. "__v" : 0,
    3. "_id" : ObjectId("56499d341af7854313c2fb8a"),
    4. "additionalProperties" : {
    5. "wifiTagDescription" : "",
    6. "networkType" : "WEP",
    7. "password" : "Ankit11",
    8. "ssid" : "ankit.chettri%40evontech.com"
    9. },
    10. "businessId" : ObjectId("55c337720545813123acf09f"),
    11. "clientId" : ObjectId("55c33660cfc3bb56068b4567"),
    12. "createdAt" : ISODate("2015-11-16T09:09:08.923Z"),
    13. "creatorAccountId" : ObjectId("55c3366672a63c6a2246af3e"),
    14. "dataChecksum" : "3d794b6847d0e7b2fcc355462b2e1d95",
    15. "deleted" : "false",
    16. "domain" : "evontech.com",
    17. "donotIndex" : false,
    18. "isDomainCard" : true,
    19. "kiTAG" : "connect#evontech.com",
    20. "lastIndexedOn" : null,
    21. "manualUpdate" : true,
    22. "name" : "connect",
    23. "relatedKeywords" : "",
    24. "seo" : true,
    25. "status" : "ACTIVE",
    26. "title" : "TEST",
    27. "type" : "Wi-Fi",
    28. "updatedAt" : ISODate("2014-12-17T06:17:17.461Z")
    29. }
    30. {
    31. "additionalProperties" : {
    32. "urlImage" : "",
    33. "urlTagDescription" : "%3Cp%3ECheck%20One%3C%2Fp%3E%0A",
    34. "url" : "http://www.test.com"
    35. },
    36. "lastIndexedOn" : null,
    37. "creatorAccountId" : ObjectId("55c3366672a63c6a2246af3e"),
    38. "clientId" : ObjectId("55c33660cfc3bb56068b4567"),
    39. "businessId" : ObjectId("55c337720545813123acf09f"),
    40. "domain" : "evontech.com",
    41. "relatedKeywords" : "",
    42. "name" : "check one",
    43. "kiTAG" : "checkone#evontech.com",
    44. "type" : "URL",
    45. "title" : "Check%20One",
    46. "_id" : ObjectId("564c42fecf3fcc200ec1806a"),
    47. "status" : "ACTIVE",
    48. "isDomainCard" : false,
    49. "donotIndex" : false,
    50. "dataChecksum" : "8bc462e65c934f6478bf0f41a6f3e30a",
    51. "manualUpdate" : true,
    52. "updatedAt" : ISODate("2015-11-18T09:21:02.296Z"),
    53. "createdAt" : ISODate("2015-11-18T09:21:02.296Z"),
    54. "deleted" : "false",
    55. "address" : [ ],
    56. "seo" : true,
    57. "__v" : 0
    58. }
    59. >

    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
Reset Password
Fill out the form below and reset your password: