MongoDB compute fields from 2 subdocuments and produce new document - mongodb

I have collection that hold 2 subdocuments fsgeneral and balancesheet. In order to compute the Book Value for different fye, I will have take the value balancesheet.shareholderFund / fsgeneral.dilutedShares.
The question is it possible to use aggregate to do this and how can this be achieved?
My input collection as below:
{
"counter": "APPLE",
"fsgeneral": {
"0": {
"fye": "Mar-01",
"dilutedShares": {
"value": 10
}
},
"1": {
"fye": "Mar-02",
"dilutedShares": {
"value": 10
}
}
},
"balancesheet": {
"0": {
"fye": "Mar-01",
"shareholderFund": {
"value": 200
}
},
"1": {
"fye": "Mar-02",
"shareholderFund": {
"value": 400
}
}
}
Expected result:
{
"counter": "APPLE",
"output": {
"0": {
"fye": "Mar-01",
"bookvalue": {
"value": 20
}
},
"1": {
"fye": "Mar-02",
"bookvalue": {
"value": 40
}
}
}
}
I have tried a few aggregates but failed to come to how 2 subdocuments can be used at the same time.

You can try,
$objectToArray convert object to array and input in $map, $map will iterate loop,
$reduce to iterate loop of fsgeneral and check key matches with balancesheet then divide using $divide
$arrayToObject, $map will return array and this will convert to again object
db.collection.aggregate([
{
$project: {
counter: 1
output: {
$arrayToObject: {
$map: {
input: { $objectToArray: "$balancesheet" },
as: "a",
in: {
k: "$$a.k",
v: {
fye: "$$a.v.fye",
bookvalue: {
value: {
$reduce: {
input: { $objectToArray: "$fsgeneral" },
initialValue: 0,
in: {
$cond: [
{ $eq: ["$$this.k", "$$a.k"] },
{ $divide: ["$$a.v.shareholderFund.value", "$$this.v.dilutedShares.value"] },
"$$value"
]
}
}
}
}
}
}
}
}
}
}
}
])
Playground

Related

Project one field from specific object element in an array mongodb

I have a collection:
{
"_id": 1,
"_deleted": false,
"customFields": [{
"fieldName": "sapID",
"value": ""
}, {
"fieldName": "salesTerritory",
"value": ""
}, {
"fieldName": "clientType",
"value": "Corporate"
}],
}
How can I project(aggregate) only the value field of the element with fieldName = "clientType":
db.collection.aggregate(
{
$project:{value:<code>}
}
)
I tried $filter but it does not work
db.collection.aggregate([
{
$project: {
"customFields.value": 1
}
}
])
mongoplayground
db.collection.aggregate([
{
$set: {
customFields: {
$map: {
input: "$customFields",
as: "c",
in: {
$cond: {
if: { "$eq": [ "$$c.fieldName", "clientType" ] },
then: { value: "$$c.value" },
else: "$$c"
}
}
}
}
}
}
])
mongoplayground
What about this?
db.collection.aggregate([
{
$project: {
customFields: {
$filter: {
input: "$customFields",
$cond: { $eq: ["$$this.fieldName", "clientType"] }
}
}
}
}
])
Mongo Playground

MongoDB aggregate: count of elements in an object key

I have a MongoDB document like this:
[
{
"_id": {
"$oid": "5ff09030cd55d6d9f378d460"
},
"username": "a value",
"userid": "123456",
"last_access_ts": 1612426253,
"last_access": "2021-2-4 9:10:53",
"anotherid": 12345678910,
"verified_date": "2021-1-2 16:24:32",
"verified_ts": 1609601072,
"group_users": {
"-1001159747589": [
{
"anotherid": 12345678910,
"userid": "123456"
}
],
"-1001143137644": [
{
"anotherid": 12345678910,
"userid": "123456"
}
],
"-1001368608972": [
{
"anotherid": 12345678910,
"userid": "123456"
}
]
},
"registered_access": "2021-1-2 16:24:42",
}
]
I've two questions.
First one: I need to count the elements inside each group_users[key] object, and I'm stuck with this aggregate:
db.collection.aggregate([
{
$match: {
username: "a value"
}
},
{
$project: {
_id: 1,
userid: 1,
"groups": {
"$objectToArray": "$group_users"
}
}
},
{
$unwind: "$groups",
},
])
This aggregate gives me this result:
[
{
"_id": ObjectId("5ff09030cd55d6d9f378d460"),
"groups": {
"k": "-1001449720492",
"v": [
{
"anotherid": 12345678910,
"userid": "123456"
}
]
},
"userid": "123456"
},
{
"_id": ObjectId("5ff09030cd55d6d9f378d460"),
"groups": {
"k": "-1001159747589",
"v": [
{
"anotherid": 12345678910,
"userid": "123456"
}
]
},
"userid": "123456"
},
{
"_id": ObjectId("5ff09030cd55d6d9f378d460"),
"groups": {
"k": "-1001143137644",
"v": [
{
"anotherid": 12345678910,
"userid": "123456"
}
]
},
"userid": "123456"
}
]
How can I count each single groups[v] and then re-group the data? I would like to have a result like:
{
... some user data
"groups": {
"group_key": "count",
"second_group_key": "count",
"third_group_key": "count"
}
}
Is it possible with aggregate or I need to loop in the code?
My second question is always about the group_users. Is possible to have, recursively, the user data inside a group_users object?
I mean, every object inside group_users is an array of users; from this array can I have the user data (maybe with $graphLookup?) using the userid field or the anotherid field?
As a result from this second aggregate I would like to have something like this:
{
... some user data
"groups": {
"group_key": [{"userid": userid, "username": username}],
"second_group_key": [{"userid": userid, "username": username}],
"third_group_key": [{"userid": userid, "username": username}]
}
}
Obviously I can limit this "recursion" to 10 elements per time.
Thanks for any advice.
$objectToArray convert group_users object to array
$let to Bind variable group_arr for use in the specified expression, and returns the result of the expression.
$map to iterate loop of bind variable group_arr, get size of total element of v and return k and v,
$arrayToObject convert returned array from $map to object
db.collection.aggregate([
{ $match: { username: "a value" } },
{
$project: {
_id: 1,
userid: 1,
groups: {
$let: {
vars: { group_arr: { $objectToArray: "$group_users" } },
in: {
$arrayToObject: {
$map: {
input: "$$group_arr",
in: {
k: "$$this.k",
v: { $size: "$$this.v" }
}
}
}
}
}
}
}
}
])
Playground
Second question,
$unwind deconstruct groups array
$lookup with pipeline, match anotherid $in condition and return required fields
$group by _id and reconstruct groups array
$arrayToObject convert groups array to object
db.collection.aggregate([
{ $match: { username: "a value" } },
{
$project: {
_id: 1,
userid: 1,
groups: { $objectToArray: "$group_users" }
}
},
{ $unwind: "$groups" },
{
$lookup: {
from: "collection",
let: { anotherid: "$groups.v.anotherid" },
pipeline: [
{ $match: { $expr: { $in: ["$anotherid", "$$anotherid"] } } },
{
$project: {
_id: 0,
userid: 1,
username: 1
}
}
],
as: "groups.v"
}
},
{
$group: {
_id: "$_id",
groups: { $push: "$groups" },
userid: { $first: "$userid" }
}
},
{ $addFields: { groups: { $arrayToObject: "$groups" } } }
])
Playground

Wildcard for key in mongodb query

I have a collection equivalent to:
[
{
"_id": ObjectId("5a934e000102030405000000"),
"sides": {
"0": {
"dist": 100
},
"1": {
"dist": 10
}
}
},
{
"_id": ObjectId("5a934e000102030405000001"),
"sides": {
"0": {
"dist": 100
}
}
}
]
I would like to perform a query that return any documents that has for any key nested in sides has the key dist with a specific value. Something like:
db.collection.find({"sides.*.dist": 10})
Here * acts as a wildcard, any key would be valid in its place.
That would retrieve:
[
{
"_id": ObjectId("5a934e000102030405000000"),
"sides": {
"0": {
"dist": 100
},
"1": {
"dist": 10
}
}
}
]
On the other hand
db.collection.find({"sides.*.dist": 100})
Would retrive both documents.
the following song and dance won't be neccessary if sides field was an array...
db.collection.find(
{
$expr: {
$gt: [{
$size: {
$filter: {
input: { $objectToArray: "$sides" },
as: "x",
cond: { $eq: ["$$x.v.dist", 10] }
}
}
}, 0]
}
})
You could get the matching elements using this
db.collection.aggregate([
{
"$project": {
"sides_array": {//Reshape the sides
"$objectToArray": "$sides"
}
}
},
{//Denormalize to get more than one matches
"$unwind": "$sides_array"
},
{
"$match": {//Condition goes here
"sides_array.v.dist": 10
}
},
{
"$group": {//Group the data back, after unwinding
"_id": "$_id",
"sides": {
"$push": "$sides_array"
}
}
},
{
"$project": {//Reshape the data
"_id": 1,
"sides": {
"$arrayToObject": "$sides"
}
}
}
])

Writing an aggregation query in MongodB

Here is my sample document structure e.g.
{
"my_object": {
"1": {
"seq": "1",
"time": "xyz",
},
"2": {
"seq": "2",
"time": "abc",
"sub_aray": {
"0": {
"value": 10
},
"1": {
"value": 10
},
"2": {
"value": -10
}
}
}
}
}
So what I want to achieve is, Sum of all the sub_array's value if exists, if sub_array isn't found, default it to 0
{"seq" : "1", "sub_array" : 0},
{"seq" : "2", "sub_array" : 10}
My Mongo version is 3.4.6 and I am using PyMongo as my driver.
You can do as below
playground
db.collection.aggregate([
{
$project: {
"array": { //To remove dynamic keys - 1,2,etc
"$objectToArray": "$my_object"
}
}
},
{//reshaping array
"$unwind": "$array"
},
{
$project: {//reshaping sub array to access via a static name
"k": {
"$objectToArray": "$array.v.sub_aray"
},
"seq": "$array.v.seq"
}
},
{
"$project": {//actual logic, output structure
"sub_array": {
$sum: "$k.v.value"
},
"_id": 0,
"seq": 1
}
}
])
The trick part is to use the $objectToArray operator to iterate my_object items.
db.collection.aggregate([
{
"$project": {
"my_object": {
"$map": {
input: {
"$objectToArray": "$my_object"
},
as: "obj",
in: {
seq: "$$obj.v.seq",
sub_array: {
$sum: {
$map: {
input: {
"$objectToArray": "$$obj.v.sub_aray"
},
as: "sub",
in: "$$sub.v.value"
}
}
}
}
}
}
}
},
{
"$unwind": "$my_object"
},
{
"$replaceRoot": {
"newRoot": "$my_object"
}
}
])
MongoPlayground

Multi condition to filter the document in mongodb according to subdocument array

I need to filter all documents by using multiple conditions.
{
"name": "Maths",
"excludeUserIds": [
{
"_id": 33,
"groupIds": [
"a",
"b",
"c"
]
}
]
},
{
"name": "Science",
"excludeUserIds": [
{
"_id": 24,
"groupIds": [
"a",
"x",
"b"
]
}
]
},
{
"name": "Health",
"excludeUserIds": []
},
{
"name": "English",
"excludeUserIds": [
{
"_id": 33,
"groupIds": []
}
]
}
The documents need to be returned if excludeUserIds._id not equal to given value AND excludeUserIds.groupIds are not in given array.
The mongo script I've written is
{
"$match": {
"excludeUserIds": {
"$elemMatch": {
"_id": {
$ne: 33
},
"groupIds": {
$nin: [
"a"
]
}
}
}
}
}
I expect this should return all document without Maths. But i doesn't work as I expected. Obviously I missed something. Thanks in advance. I created a Mongo playground
If the requirement is to return all subjects except Maths, your logic should be
if excludeUserIds._id not equal to given value AND OR excludeUserIds.groupIds are not in given array.
Which can be translated to the following code, which convert the logic from !A || !B to the equivalent !(A && B) for shorter code.
db.collection.aggregate([
{
$match: {
excludeUserIds: {
$not: {
$elemMatch: {
_id: 33,
groupIds: { $all: ["a"] }
}
}
}
}
}
])
Playground
If you only need to match excludeUserIds.groupIds against one value, instead of an array, you can do the following
db.collection.aggregate([
{
$match: {
excludeUserIds: {
$not: {
$elemMatch: {
_id: 33,
groupIds: "a"
}
}
}
}
}
])
Playground
Try using this
db.collection.find({
"excludeUserIds._id": { $ne: 33 },
"excludeUserIds.groupIds": { $nin: ["a"] }
})
This is probably not the solution but will give required result.
db.collection.aggregate([
{
"$match": {
"excludeUserIds": {
"$elemMatch": {
"_id": {
$ne: 33
},
"groupIds": {
"$elemMatch": {
$nin: [
"a"
]
}
}
}
}
}
}
])
Play
You need to use $elemMatch for nested array as well.
EDIT:
Updated Play
db.collection.aggregate([
{
"$match": {
"$or": [
{
"excludeUserIds": {
"$elemMatch": {
"_id": {
$ne: 33
},
"groupIds": {
"$elemMatch": {
$nin: [
"a"
]
}
}
}
}
},
{
"$or": [
{
excludeUserIds: {
$exists: false
}
},
{
excludeUserIds: null
},
{
excludeUserIds: {
$size: 0
}
}
]
},
{
"$or": [
{
"excludeUserIds.groupIds": {
$exists: false
}
},
{
"excludeUserIds.groupIds": null
},
{
"excludeUserIds.groupIds": {
$size: 0
}
}
]
}
]
}
}
])
You can check for existence/empty to get those as well.