mongoDB $sort 不一致的结果

huangapple go评论177阅读模式
英文:

mongoDB $sort inconsistent results

问题

我在go语言中有以下查询,运行良好:

  1. query["name"] = bson.M{"$regex": searchStr, "$options": "i"}
  2. query["likes"] = userSession.Id
  3. c.Find(query).Skip(0).Limit(2).Select(bson.M{"name":1, "profile":1, "description":1, "user_id":1, "likes":1}).Sort("-pro", "-check").All(&business)

然后我尝试使用聚合框架编写相同的查询:

  1. query["name"] = bson.M{"$regex": searchStr, "$options": "i"}
  2. query["likes"] = userSession.Id
  3. oe := bson.M{
  4. "$match" :query,
  5. }
  6. oa := bson.M{
  7. "$project": bson.M {"pro": 1, "check": 1, "name":1, "profile":1, "description":1, "user_id":1, "likes":1, "nrLikes": bson.M{ "$size": "$likes" }, "city": 1, "country": 1, "industry": 1},
  8. }
  9. ol := bson.M{
  10. "$limit" :pageSize,
  11. }
  12. os := bson.M{
  13. "$skip" :skips,
  14. }
  15. or := bson.M{
  16. "$sort" : bson.M {"pro": -1, "check": -1},
  17. }
  18. pipe := c.Pipe([]bson.M{oe, oa, or, os, ol })
  19. pipe.All(&business)

第二个查询大部分时间都能正常工作,但有10%的时间会返回不同顺序的结果。

有什么想法吗?

稍后编辑:以下是结果:

  1. []bson.M{
  2. {
  3. "description": "<p>sasdfdasf</p>",
  4. "profile": []interface {}{
  5. "rKwMmXPWheGczwvGn2TzSRU7jRorhorKwMmXPWheGczwvGn2TzSRU7jRorho=0.jpg",
  6. },
  7. "likes": []interface {}{
  8. "Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9",
  9. },
  10. "nrLikes": int(1),
  11. "name": "ediloc.com2",
  12. "city": "Calimanesti",
  13. "industry": "Automotive",
  14. "_id": "Yo\xd4f\x1a\xa9Q|w\tG^",
  15. "user_id": "Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9",
  16. "country": "Romania",
  17. },
  18. {
  19. "_id": "Yo\xc7\xd7\x1a\xa9Qy1[&#39;\xea",
  20. "user_id": "Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9",
  21. "name": "ediloc.com",
  22. "country": "Romania",
  23. "description": "<p>a</p>",
  24. "profile": []interface {}{
  25. "1ssSySNRZwGJJwqzXghL6qzAVfWZis1ssSySNRZwGJJwqzXghL6qzAVfWZis=1.jpg",
  26. },
  27. "likes": []interface {}{
  28. "Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9",
  29. },
  30. "nrLikes": int(1),
  31. "city": "Calimanesti",
  32. "industry": "Accounting",
  33. },
  34. }
  35. []bson.M{
  36. {
  37. "likes": []interface {}{
  38. "Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9",
  39. },
  40. "_id": "Yo\xd4f\x1a\xa9Q|w\tG^",
  41. "name": "ediloc.com2",
  42. "city": "Calimanesti",
  43. "country": "Romania",
  44. "profile": []interface {}{
  45. "rKwMmXPWheGczwvGn2TzSRU7jRorhorKwMmXPWheGczwvGn2TzSRU7jRorho=0.jpg",
  46. },
  47. "user_id": "Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9",
  48. "industry": "Automotive",
  49. "nrLikes": int(1),
  50. },
  51. {
  52. "_id": "Yo\xc7\xd7\x1a\xa9Qy1[&#39;\xea",
  53. "user_id": "Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9",
  54. "industry": "Accounting",
  55. "profile": []interface {}{
  56. "1ssSySNRZwGJJwqzXghL6qzAVfWZis1ssSySNRZwGJJwqzXghL6qzAVfWZis=1.jpg",
  57. },
  58. "likes": []interface {}{
  59. "Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9",
  60. },
  61. "nrLikes": int(1),
  62. "name": "ediloc.com",
  63. "city": "Calimanesti",
  64. "country": "Romania",
  65. "description": "<p>a</p>",
  66. },
  67. }
  68. []bson.M{
  69. {
  70. "nrLikes": int(1),
  71. "user_id": "Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9",
  72. "description": "<p>a</p>",
  73. "profile": []interface {}{
  74. "1ssSySNRZwGJJwqzXghL6qzAVfWZis1ssSySNRZwGJJwqzXghL6qzAVfWZis=1.jpg",
  75. },
  76. "country": "Romania",
  77. "industry": "Accounting",
  78. "likes": []interface {}{
  79. "Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9",
  80. },
  81. "_id": "Yo\xc7\xd7\x1a\xa9Qy1[&#39;\xea",
  82. "name": "ediloc.com",
  83. "city": "Calimanesti",
  84. },
  85. {
  86. "name": "ediloc.com2",
  87. "industry": "Automotive",
  88. "description": "<p>sasdfdasf</p>",
  89. "likes": []interface {}{
  90. "Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9",
  91. },
  92. "user_id": "Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9",
  93. "city": "Calimanesti",
  94. "country": "Romania",
  95. "profile": []interface {}{
  96. "rKwMmXPWheGczwvGn2TzSRU7jRorhorKwMmXPWheGczwvGn2TzSRU7jRorho=0.jpg",
  97. },
  98. "nrLikes": int(1),
  99. "_id": "Yo\xd4f\x1a\xa9Q|w\tG^",
  100. },
  101. }
  102. procheck字段是int32类型具有较高pro字段编号的文档应优先于具有较高check字段的文档
  103. <details>
  104. <summary>英文:</summary>
  105. I have the following query in go lang which works fine:
  106. query[&quot;name&quot;] = bson.M{&quot;$regex&quot;: searchStr, &quot;$options&quot;: &quot;i&quot;}
  107. query[&quot;likes&quot;] = userSession.Id
  108. c.Find(query).Skip(0).Limit(2).Select(bson.M{&quot;name&quot;:1, &quot;profile&quot;:1, &quot;description&quot;:1, &quot;user_id&quot;:1, &quot;likes&quot;:1}).Sort(&quot;-pro&quot;, &quot;-check&quot;).All(&amp;business);
  109. Then I tried to write the same query using the aggregation framework:
  110. query[&quot;name&quot;] = bson.M{&quot;$regex&quot;: searchStr, &quot;$options&quot;: &quot;i&quot;}
  111. query[&quot;likes&quot;] = userSession.Id
  112. oe := bson.M{
  113. &quot;$match&quot; :query,
  114. }
  115. oa := bson.M{
  116. &quot;$project&quot;: bson.M {&quot;pro&quot;: 1, &quot;check&quot;: 1, &quot;name&quot;:1, &quot;profile&quot;:1, &quot;description&quot;:1, &quot;user_id&quot;:1, &quot;likes&quot;:1, &quot;nrLikes&quot;: bson.M{ &quot;$size&quot;: &quot;$likes&quot; }, &quot;city&quot;: 1, &quot;country&quot;: 1, &quot;industry&quot;: 1},
  117. }
  118. ol := bson.M{
  119. &quot;$limit&quot; :pageSize,
  120. }
  121. os := bson.M{
  122. &quot;$skip&quot; :skips,
  123. }
  124. or := bson.M{
  125. &quot;$sort&quot; : bson.M {&quot;pro&quot;: -1, &quot;check&quot;: -1},
  126. }
  127. pipe := c.Pipe([]bson.M{oe, oa, or, os, ol })
  128. pipe.All(&amp;business)
  129. The second query works fine 90% of the time, but 10% of the times it returns a different order for results.
  130. Any thoughts?
  131. Later edit: Here are the resuls
  132. []bson.M{
  133. {
  134. &quot;description&quot;: &quot;&lt;p&gt;sasdfdasf&lt;/p&gt;&quot;,
  135. &quot;profile&quot;: []interface {}{
  136. &quot;rKwMmXPWheGczwvGn2TzSRU7jRorhorKwMmXPWheGczwvGn2TzSRU7jRorho=0.jpg&quot;,
  137. },
  138. &quot;likes&quot;: []interface {}{
  139. &quot;Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9&quot;,
  140. },
  141. &quot;nrLikes&quot;: int(1),
  142. &quot;name&quot;: &quot;ediloc.com2&quot;,
  143. &quot;city&quot;: &quot;Calimanesti&quot;,
  144. &quot;industry&quot;: &quot;Automotive&quot;,
  145. &quot;_id&quot;: &quot;Yo\xd4f\x1a\xa9Q|w\tG^&quot;,
  146. &quot;user_id&quot;: &quot;Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9&quot;,
  147. &quot;country&quot;: &quot;Romania&quot;,
  148. },
  149. {
  150. &quot;_id&quot;: &quot;Yo\xc7\xd7\x1a\xa9Qy1[&#39;\xea&quot;,
  151. &quot;user_id&quot;: &quot;Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9&quot;,
  152. &quot;name&quot;: &quot;ediloc.com&quot;,
  153. &quot;country&quot;: &quot;Romania&quot;,
  154. &quot;description&quot;: &quot;&lt;p&gt;a&lt;/p&gt;&quot;,
  155. &quot;profile&quot;: []interface {}{
  156. &quot;1ssSySNRZwGJJwqzXghL6qzAVfWZis1ssSySNRZwGJJwqzXghL6qzAVfWZis=1.jpg&quot;,
  157. },
  158. &quot;likes&quot;: []interface {}{
  159. &quot;Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9&quot;,
  160. },
  161. &quot;nrLikes&quot;: int(1),
  162. &quot;city&quot;: &quot;Calimanesti&quot;,
  163. &quot;industry&quot;: &quot;Accounting&quot;,
  164. },
  165. }
  166. []bson.M{
  167. {
  168. &quot;likes&quot;: []interface {}{
  169. &quot;Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9&quot;,
  170. },
  171. &quot;_id&quot;: &quot;Yo\xd4f\x1a\xa9Q|w\tG^&quot;,
  172. &quot;name&quot;: &quot;ediloc.com2&quot;,
  173. &quot;city&quot;: &quot;Calimanesti&quot;,
  174. &quot;country&quot;: &quot;Romania&quot;,
  175. &quot;profile&quot;: []interface {}{
  176. &quot;rKwMmXPWheGczwvGn2TzSRU7jRorhorKwMmXPWheGczwvGn2TzSRU7jRorho=0.jpg&quot;,
  177. },
  178. &quot;user_id&quot;: &quot;Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9&quot;,
  179. &quot;industry&quot;: &quot;Automotive&quot;,,
  180. &quot;nrLikes&quot;: int(1),
  181. },
  182. {
  183. &quot;_id&quot;: &quot;Yo\xc7\xd7\x1a\xa9Qy1[&#39;\xea&quot;,
  184. &quot;user_id&quot;: &quot;Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9&quot;,
  185. &quot;industry&quot;: &quot;Accounting&quot;,
  186. &quot;profile&quot;: []interface {}{
  187. &quot;1ssSySNRZwGJJwqzXghL6qzAVfWZis1ssSySNRZwGJJwqzXghL6qzAVfWZis=1.jpg&quot;,
  188. },
  189. &quot;likes&quot;: []interface {}{
  190. &quot;Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9&quot;,
  191. },
  192. &quot;nrLikes&quot;: int(1),
  193. &quot;name&quot;: &quot;ediloc.com&quot;,
  194. &quot;city&quot;: &quot;Calimanesti&quot;,
  195. &quot;country&quot;: &quot;Romania&quot;,
  196. &quot;description&quot;: &quot;&lt;p&gt;a&lt;/p&gt;&quot;,
  197. },
  198. }
  199. []bson.M{
  200. {
  201. &quot;nrLikes&quot;: int(1),
  202. &quot;user_id&quot;: &quot;Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9&quot;,
  203. &quot;description&quot;: &quot;&lt;p&gt;a&lt;/p&gt;&quot;,
  204. &quot;profile&quot;: []interface {}{
  205. &quot;1ssSySNRZwGJJwqzXghL6qzAVfWZis1ssSySNRZwGJJwqzXghL6qzAVfWZis=1.jpg&quot;,
  206. },
  207. &quot;country&quot;: &quot;Romania&quot;,
  208. &quot;industry&quot;: &quot;Accounting&quot;,
  209. &quot;likes&quot;: []interface {}{
  210. &quot;Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9&quot;,
  211. },
  212. &quot;_id&quot;: &quot;Yo\xc7\xd7\x1a\xa9Qy1[&#39;\xea&quot;,
  213. &quot;name&quot;: &quot;ediloc.com&quot;,
  214. &quot;city&quot;: &quot;Calimanesti&quot;,
  215. },
  216. {
  217. &quot;name&quot;: &quot;ediloc.com2&quot;,
  218. &quot;industry&quot;: &quot;Automotive&quot;,
  219. &quot;description&quot;: &quot;&lt;p&gt;sasdfdasf&lt;/p&gt;&quot;,
  220. &quot;likes&quot;: []interface {}{
  221. &quot;Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9&quot;,
  222. },
  223. &quot;user_id&quot;: &quot;Yo\xc7;\x1a\xa9Qy\b\xb8\xa2\xf9&quot;,
  224. &quot;city&quot;: &quot;Calimanesti&quot;,
  225. &quot;country&quot;: &quot;Romania&quot;,
  226. &quot;profile&quot;: []interface {}{
  227. &quot;rKwMmXPWheGczwvGn2TzSRU7jRorhorKwMmXPWheGczwvGn2TzSRU7jRorho=0.jpg&quot;,
  228. },
  229. &quot;nrLikes&quot;: int(1),
  230. &quot;_id&quot;: &quot;Yo\xd4f\x1a\xa9Q|w\tG^&quot;,
  231. },
  232. }
  233. Pro and check fields are in32, the documents with higher pro field number should have priority over the documents that have higher check fields.
  234. </details>
  235. # 答案1
  236. **得分**: 2
  237. 确保在限制和跳过阶段之前设置排序管道阶段只有在排序输入上才能可靠地获得相同的结果
  238. **编辑**
  239. 意识到您正在使用`bson.M {&quot;pro&quot;: -1, &quot;check&quot;: -1}`来定义排序顺序Go映射的迭代顺序是未指定的可能会发生变化这可能是您得到不一致结果的原因
  240. 尝试将其更改为`bson.D`以便保持按列排序的顺序
  241. 可以查看查询[Sort](http://bazaar.launchpad.net/+branch/mgo/v2/view/head:/session.go#L2130)方法如何从提供的字符串构建它。
  242. 对于您的用例您可以将`or`变量更改为
  243. or := bson.M{
  244. &quot;$sort&quot;: bson.D{
  245. bson.DocElem{Name: &quot;pro&quot;, Value: -1},
  246. bson.DocElem{Name: &quot;check&quot;, Value: -1},
  247. },
  248. }
  249. <details>
  250. <summary>英文:</summary>
  251. Make sure you have your sort pipeline stage *before* your limit &amp; skip stages. You can only reliably obtain the same results with limit/skip on sorted input.
  252. **EDIT**
  253. Realised that you are using `bson.M {&quot;pro&quot;: -1, &quot;check&quot;: -1}` to define your sort order. The iteration order of a map is unspecified in Go and can change. Hence this is probably why you are getting inconsistent results.
  254. Try changing this to a `bson.D` so that the order of columns to sort by is maintained.
  255. It make help to see how the query [Sort](http://bazaar.launchpad.net/+branch/mgo/v2/view/head:/session.go#L2130) method constructs this from the strings you provide.
  256. For your use case, you would change the `or` variable to:
  257. or := bson.M{
  258. &quot;$sort&quot;: bson.D{
  259. bson.DocElem{Name: &quot;pro&quot;, Value: -1},
  260. bson.DocElem{Name: &quot;check&quot;, Value: -1},
  261. },
  262. }
  263. </details>

huangapple
  • 本文由 发表于 2017年7月26日 07:08:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/45314871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定