Hello,
I have these a database with entries looking like this:
{ "_id" : BinData(0,"gQAAACGewgAAAAAAvP+DHA=="), "statx" : { "atime" : { "sec" : NumberLong(1727861248), "nsec" : 519788713 }, "blksize" : 4096, "blocks" : NumberLong(0), "btime" : { "sec" : NumberLong(1727861248), "nsec" : 519788713 }, "ctime" : { "sec" : NumberLong(1727861267), "nsec" : 939788713 }, "dev" : { "major" : 252, "minor" : 1 }, "gid" : 0, "ino" : NumberLong(12754465), "mode" : 493, "mtime" : { "sec" : NumberLong(1727861267), "nsec" : 939788713 }, "nlink" : 2, "rdev" : { "major" : 0, "minor" : 0 }, "size" : NumberLong(33), "type" : 16384, "uid" : 0 }, "ns" : [ { "parent" : BinData(0,"gQAAAIEAQAAAAAAAVIeqAw=="), "name" : "bidule", "xattrs" : { "path" : "/bidule" } } ] }
{ "_id" : BinData(0,"gQAAAH2VHAEAAAAABleStw=="), "statx" : { "atime" : { "sec" : NumberLong(1727861260), "nsec" : 410788713 }, "blksize" : 4096, "blocks" : NumberLong(0), "btime" : { "sec" : NumberLong(1727861260), "nsec" : 410788713 }, "ctime" : { "sec" : NumberLong(1727861260), "nsec" : 410788713 }, "dev" : { "major" : 252, "minor" : 1 }, "gid" : 0, "ino" : NumberLong(18650493), "mode" : 420, "mtime" : { "sec" : NumberLong(1727861260), "nsec" : 410788713 }, "nlink" : 1, "rdev" : { "major" : 0, "minor" : 0 }, "size" : NumberLong(100), "type" : 32768, "uid" : 0 }, "ns" : [ { "parent" : BinData(0,"gQAAACGewgAAAAAAvP+DHA=="), "name" : "1", "xattrs" : { "path" : "/bidule/1" } } ] }
`
And I would like to get the sum of all `statx.size` for all entries with a `ns.xattrs.path` like `/bidule`. I managed to do it easily with a condition on `statx.ino` for instance, but I can't figure out a proper way to do it for a string in an array. More specifically,
1. I'm not exactly sure what's the proper way to checking equality between strings, as I've seen the use of `$eq`, `$strcasecmp` or simply `<key>: <value>`.
2. I don't know how to query an item in an array for aggregate requests.
I've tried these commands, but they are either malformed and I don't know the reason, or the result is invalid:
`
db.entries.aggregate([{$group: {_id: '', test: { $sum: {$filter: {input: "$ns", as: "link", cond: [{$eq: [ "$$link.name", "1"]}, '$statx.size', 0] }}}}}, { $project: { _id:0, somme: '$test'}}])
db.entries.aggregate({$group: {_id: '', test: { $sum: {$cond: [{$strcasecmp: ["$ns.name", "1"]}, '$statx.size', 0]} }}}, { $project: { _id:0, somme: '$test'}})
db.entries.aggregate({$group: {_id: '', test: { $sum: {$cond: [{'ns.name': '1'}, '$statx.size', 0]} }}}, { $project: { _id:0, somme: '$test'}}) })
`
Does anyone know how to do this ?