Order of results differs for same query in golang and node js

Same Query response differ in golang and node js, WHY?

Welcome to the MongoDB community @Aman_Saxena !

Please provide some more details for this issue:

  • Query and response for each driver
  • MongoDB driver versions used
  • MongoDB server version

Thanks,
Stennie

Query := {
		"_id.src": {
			"$in": fids,
		},
		"_id.trg":{
			"$in": bids,
		},
	}

id is object of trg(int64) anf src(int64)
mongoDB Driver golang = mongo-driver v1.10.1
mongoDB driver in nodejs latest version 4.10.0

server version is 4.4.1
@Stennie_X
So ordering of data which I am getting is different?

If I understand correctly the following

it means you get the same documents but not in the same order. If you want a deterministic order in your documents you must sort.

1 Like

Ok, I got it but is there any predefined order of documents for different drivers?

There is no predetermined order of documents returned. Even using the same driver documents could be returned in a different order on consecutive calls.

As @steevej mentions, if you want the documents returned in the same order everywhere, then provide a sort. One thing to note here is if the sort does not provide unique values to be sorted on, you could still see documents returned in different ordering for the values that are the same.

2 Likes

As a work around you may always add _id:1 as the last multi-key sort to ensure order when sort prefix has same values. _id is unique so final order will be deterministic.

3 Likes