Version Comparison in MongoDB

just wondering if anyone can come up with a smarter way to do this ...

Say I have some documents in MongoDB with a version property, for example, something like { ..., version: { major: 1, minor: 2, patch: 13 } } or { ..., version: "1.2.13" } if that is easier.

What is the easiest way to find all documents with version XYZ or higher? The way I am doing this now is basically a huge $ and offer.

+4
source share
2 answers

Your first design is actually very good. The reason is that you can index major , minor and patch fields for quick reading.

Querying the data is actually a lot easier than using the $and query. You can simply use the basic query for matching fields:

eg.

 db.versions.find({major:1,minor:2,patch:20}) //give me documents of version 1.2.20 db.versions.find({major:1}) //give me all documents with major version 1 db.versions.find({major:1,minor:{$gte:2,$lte:5}}) //give me all documents of major version and minor versions between 2 and 5 inclusive. 

You can create an index, for example

 db.versions.ensureIndex({major:1,minor:1,patch:1}) 

The $and query is mainly intended for the case when you want to run a more complex query in the same field several times. For example, if you want to get major versions 1 and 2, you can use $and .

db.versions.find({major:1,major:2}) will actually return documents with major version 2. However, db.versions.find({a:$all:[1,2]}) will also work in this case . Ideally, you should avoid using $and if possible, because it actually generates multiple queries for each expression in the $and array and does the join. This is much more expensive than the sample requests above.

+1
source

If you use a fixed number of digits for the version number and save it as a string, you can use $gte .

 "001.002.0013" > "001.002.0011" = true "001.003.0013" > "001.002.0013" = true "002.000.0000" > "001.002.0013" = true "001.002.0012" > "001.002.0013" = false "001.001.0013" > "001.002.0013" = false 
+1
source

Source: https://habr.com/ru/post/1492550/


All Articles