亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

nosql - mongoDB的排序問題
大家講道理
大家講道理 2017-04-21 10:56:29
0
4
870

有一個(gè)List ArrayList<Long> al=new ArrayList<Long>();里面存儲了從外部獲取的數(shù)據(jù)的ID并且進(jìn)行了排序(隨機(jī)抽取的ID,按照先后放入LIST),然后把這些ID在mongoDB通過inObj.put("_id", new BasicDBObject("$in", list));進(jìn)行in查詢,獲取后的結(jié)果并不和LIST中ID的順序一樣,搜索了一下,有文章說不指定排序的話,mongodb會按照$natural進(jìn)行排序,但是我測試了一下,并不是這樣。
現(xiàn)在問題是怎么才能讓mongodb按照list的排序獲取結(jié)果呢

大家講道理
大家講道理

光陰似箭催人老,日月如移越少年。

reply all(4)
伊謝爾倫

This is a great question. In fact, the article you checked is not wrong. If you don’t specify the order, it is arranged according to the natural order. It’s just that this natural sorting means to sort the data according to its own sorting method after finding the data within the range of _id的自然排序去排。你的list只是一個(gè)查詢條件,就相當(dāng)于x > max AND x < min指定的一個(gè)查詢范圍,跟排序完全沒有關(guān)系,不論是什么數(shù)據(jù)庫系統(tǒng),都會在你指定的這個(gè)list.

My suggestion is that you still get the value in this way. After taking it out, loop it through the java program yourself, and then run out the sorted array.

伊謝爾倫

@joyqi’s statement is not quite right. Natural sorting in MongoDB (that is, sorting by the $natural field) is different from sorting by the _id field. Natural sorting is based on the order in which data is organized in the data file.

You can explain your query and see how it uses the index. For example, if you use the following statement to insert data:

> db.test.insert({a:2,b:1})
> db.test.insert({a:1,b:2})
> db.test.insert({a:2,b:3})
> db.test.insert({a:1,b:4})

When you do not index the a field, you will get the following results. The following is the result of natural sorting:

> db.test.find({a:{$in:[1,2]}})
{ "_id" : ObjectId("4f509380ea72116cb2137567"), "a" : 2, "b" : 1 }
{ "_id" : ObjectId("4f509380ea72116cb2137568"), "a" : 1, "b" : 2 }
{ "_id" : ObjectId("4f509380ea72116cb2137569"), "a" : 2, "b" : 3 }
{ "_id" : ObjectId("4f509380ea72116cb213756a"), "a" : 1, "b" : 4 }

Because MongoDB is a table query at this time, if a piece of data is scanned and meets the conditions, it is added to the return list. So it's done in natural order.

And if you add the index and check again, you will get the following results

> db.test.ensureIndex({a:1})
> db.test.find({a:{$in:[1,2]}})
{ "_id" : ObjectId("4f509537ea72116cb213756f"), "a" : 1, "b" : 4 }
{ "_id" : ObjectId("4f509530ea72116cb213756d"), "a" : 1, "b" : 2 }
{ "_id" : ObjectId("4f509534ea72116cb213756e"), "a" : 2, "b" : 3 }
{ "_id" : ObjectId("4f50952cea72116cb213756c"), "a" : 2, "b" : 1 }

You can see that the results are separated according to a being 1 and a being 2, because when querying by index, it is equivalent to checking multiple pieces of data in the index. In the example here, it is equivalent to checking that the range of a is [1,1] and [2,2], these two times are logically serial, first check the data of [1,1], and then check the data of [2,2], so here a is 1 and 2 's separated.

伊謝爾倫

I have never used mongodb, but I feel that the answer to this question is the same as other relational databases. If you insert an ID like this in a relational database, and the ID is the primary key, the database will automatically sort according to the value of the ID, because the primary key itself is a clustered index. It is very simple to achieve the effect you want. Do not generate it yourself. ID, let Mongodb generate an automatically incrementing ID, so that the sorting is based on this auto-incrementing value when inserting, and when you read it, it is also read in the order of insertion.

左手右手慢動作

Search and rank them!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template