logo
Tags down

shadow

MongoDB version 3.4 string number sorting with aggregate


By : Drew P
Date : August 01 2020, 09:00 AM
this one helps. I've product sku number in string format like "500011" and I want to sort get all product with sku descending sort in mongoDB 3.4 version. , As Joe have mentioned the collation may help you here.
code :
db.products.aggregate(
[{$sort: {"sku": -1}}], 
{collation: {locale: "en", numericOrdering: true}})
{ "_id" : ObjectId("5e58d89fe0aba047483c3bbc"), "sku" : "500012" }
{ "_id" : ObjectId("5e58d89de0aba047483c3bbb"), "sku" : "500011" }
{ "_id" : ObjectId("5e58d8a3e0aba047483c3bbe"), "sku" : "404012" }
{ "_id" : ObjectId("5e58d8a0e0aba047483c3bbd"), "sku" : "400012" }
{ "_id" : ObjectId("5e58d8abe0aba047483c3bbf"), "sku" : "90000" }
{ "_id" : ObjectId("5e58d8aee0aba047483c3bc0"), "sku" : "1000" }


Share : facebook icon twitter icon

Sorting number values stored as a BigDecimal type with Spring Data MongoDB in MongoDB


By : Robert Balick
Date : March 29 2020, 07:55 AM
like below fixes the issue Unfortunately, BigDecimal is not supported by MongoDB natively. That's why we convert it into Strings by default.
For a more numeric handling, especially if you deal with prices or the like, we usually recommend to rather store doubles instead and convert them into BigDecimals for arithmetic operations in the domain object directly.

MongoDB Aggregate: Sorting by existing values first


By : user1938584
Date : March 29 2020, 07:55 AM
should help you out You can use $setIntersection to extract the matching elements followed by $size to count the matches.
$sort descending documents on number of matches.
code :
Video.aggregate([ 
  {"$addFields":{ "numofmatches":{"$size":{"$setIntersection":["$categories", req.user.interestedIn]}}}}, 
  {"$sort":{"numofmatches":-1}} 
])

Is Amazon DocDB aggregate sorting dfiferent from MongoDB?


By : dino di lemma
Date : March 29 2020, 07:55 AM
With these it helps Based on MongoDB documentation: https://docs.mongodb.com/manual/reference/operator/aggregation/group/ it is stated that $group does not order its output documents.
Even in the example they provide they $sort it as the last part of the aggregation.

How to find the highest number by sorting inside alphanumerical string in MongoDB


By : user3634752
Date : March 29 2020, 07:55 AM
around this issue That's because of MongoDB's string comparison, if I'm not wrong you might be getting this order "L99" > "L9" > "L100" , cause it would check letter by letter in a given string, So Since L matches in all documents then it would go for next letter that way it will give you above order.
1) In case all of documents has LsNr starts with L try this :
code :
collection.aggregate([
/** $match similar to .find() */
{ $match: { filter } },
/** $addFields to add a new field 'number' which bring number out of 'L9' -> 9 */
{ $addFields: { number: { $toInt: { $substr: ["$LsNr", 1, -1] } } } },
/** sort number & $project to remove newly created field 'number' */
{ $sort: { number: -1 } }, { $project: { number: 0 } }, {$limit: 1}
])
collection.aggregate([
    { $match: { filter } }, // Your filter
    { $addFields: { letter: { $substr: ["$LsNr", 0, 1] }, number: { $toInt: { $substr: ["$LsNr", 1, -1] } } } },
    { $sort: { number: -1 } }, { $sort: { letter: -1 } },
    { $project: { letter: 0, number: 0 } }])

MongoDB aggregate and project total number of results


By : user5424492
Date : March 29 2020, 07:55 AM
I wish this help you Yes, it is possible, the grouped documents can be accessed with $push
shadow
Privacy Policy - Terms - Contact Us © voile276.org