mongodb aggregation project objectId with concat - mongodb

db.test.aggregate({
$match : { "themType" : "SuperTest" , "mType" : { "$in" : [ 1 , 2]}}
},
{ $project : { "_id" : 1, "refTestId" : 1, "avatar" : { $concat : [$refTestId] }
} });
and avatar returns me null, probably its because its objectId, is it possible in this query to make from this objectId string ?

From MongoDB 4.0 and newer, there is a $toString operator which returns the ObjectId value as a hexadecimal string:
db.test.aggregate([
{ "$match": {
"themType": "SuperTest",
"mType": { "$in" : [1 , 2] }
} },
{ "$addFields": {
"avatar": { "$toString": "$refTestId" }
} }
])
or using $convert
db.test.aggregate([
{ "$match": {
"themType": "SuperTest",
"mType": { "$in" : [1 , 2] }
} },
{ "$addFields": {
"avatar": {
"$convert": { "input": "$refTestId", "to": "string" }
}
} }
])

This isn't possible yet. WiP issue see: https://jira.mongodb.org/browse/SERVER-29512

Related

MongoDB $cond with embedded document array

I am trying to generate a new collection with a field 'desc' having into account a condition in field in a documment array. To do so, I am using $cond statement
The origin collection example is the next one:
{
"_id" : ObjectId("5e8ef9a23e4f255bb41b9b40"),
"Brand" : {
"models" : [
{
"name" : "AA"
},
{
"name" : "BB"
}
]
}
}
{
"_id" : ObjectId("5e8ef9a83e4f255bb41b9b41"),
"Brand" : {
"models" : [
{
"name" : "AG"
},
{
"name" : "AA"
}
]
}
}
The query is the next:
db.runCommand({
aggregate: 'cars',
'pipeline': [
{
'$project': {
'desc': {
'$cond': {
if: {
$in: ['$Brand.models.name',['BB','TC','TS']]
},
then: 'Good',
else: 'Bad'
}
}
}
},
{
'$project': {
'desc': 1
}
},
{
$out: 'cars_stg'
}
],
'allowDiskUse': true,
})
The problem is that the $cond statement is always returning the "else" value. I also have tried $or statement with $eq or the $and with $ne, but is always returning "else".
What am I doing wrong, or how should I fix this?
Thanks
Since $Brand.models.name returns an array, we cannot use $in operator.
Instead, we can use $setIntersection which returns an array that contains the elements that appear in every input array
db.cars.aggregate([
{
"$project": {
"desc": {
"$cond": [
{
$gt: [
{
$size: {
$setIntersection: [
"$Brand.models.name",
[
"BB",
"TC",
"TS"
]
]
}
},
0
]
},
"Good",
"Bad"
]
}
}
},
{
"$project": {
"desc": 1
}
},
{
$out: 'cars_stg'
}
])
MongoPlayground | Alternative $reduce

How to get values in mongdb

This is my my data in Mongodb
{
"d" : {
"results" : [
{
"slack_id" : "RAGHU#TN.COM",
"connector_id" : "GRECLNT900",
"sys_role" : "DEV",
"user_id" : "RAGHU"
},
{
"slack_id" : "RAGHU#TN.COM",
"connector_id" : "GRECLNT900",
"sys_role" : "PRD",
"user_id" : "RAGHU",
"question" : "What is your favorite color?",
"Answer" : "Orange"
},
]
}
}
If i am giving RAGHU#TN.COM. then i want display sys_role. Output like this[DEV, PRD]
I am trying this way
x = mydb.mycollection.distinct("sys-role")
But I get an empty array like [ ]
You have to treat the cursor as a reference(personally I see it as a reference in C), and then de-reference it to see the result.(What is inside the address)
For the specific column, here is the result from command prompt:
my_cursor = mydb.mycollection.distinct("sys-role")
for x in my_cursor:
print('{0}'.format(x['sys_role']))
The distinct operator is not inter-operatable thus it's hard to filter by slack_id first. I would recommande using aggregation pipelines.
Here is an example.
[
{
'$match': {
'slack_id': 'RAGHU#TN.COM'
}
}, {
'$group': {
'_id': 'slack_id',
'result': {
'$addToSet': 'sys_role'
}
}
}
]
With this pipeline, your sys_role set will be in the .result field.
Using Mongo aggregation query you will get required result set. Try this:
db.collection.aggregate([
{
"$match": {
"d.results.slack_id": "RAGHU#TN.COM"
}
},
{
$group: {
_id: "$d.results.slack_id",
sys_role: {
$push: "$d.results.sys_role"
}
}
}
])
db.getCollection("collection").aggregate(
// Pipeline
[
// Stage 1
{
$project: {
results: {
$filter: {
input: "$d.results",
as: "item",
cond: { $eq: [ "$$item.slack_id", 'RAGHU#TN.COM' ] }
}
}
}
},
// Stage 2
{
$unwind: {
path : "$results",
preserveNullAndEmptyArrays : false // optional
}
},
// Stage 3
{
$group: {
_id:'$results.slack_id',
sys_roles:{$addToSet:'$results.sys_role'}
}
},
]
);

Mongoid duplicate fields query causes FieldPath field names may not contain '.' [duplicate]

I have the following mongo data which looks like this
{
eventType : "mousedown",
eventArgs : {
type : "touchstart",
elementId : "id1"
},
creationDateTime : ISODate("2017-02-24T07:05:49.986Z")
}
I wrote the following query to perform group count.
db.analytics.aggregate
(
{
$match :
{
$and :
[
{"eventArgs.type" : 'touchstart'},
{eventType : 'mousedown'},
{creationDateTime : {$gte : ISODate("2017-02-24T000:00:00.000Z")}}
]
}
},
{
$group :
{
_id :
{
"eventsArgs.elementId" : "$elementId"
},
count :
{
$sum : 1
}
}
}
);
I'm getting error for $group, which states that
FieldPath field names may not contain '.'
If I were not able to specific '.' in
$group :
{
_id :
{
"eventsArgs.elementId" : "$elementId"
},
What is the correct way to do so?
Since you have a single group field, the best way is to just use the _id group key on that field and then create another $project pipeline that will reshape the _id key from the previous pipeline into the desired subdocument that you want. For example
db.analytics.aggregate([
{
"$match": {
"eventArgs.type": 'touchstart',
"eventType": 'mousedown',
"creationDateTime": { "$gte": ISODate("2017-02-24T000:00:00.000Z") }
}
},
{
"$group": {
"_id": "$eventArgs.elementId",
"count": { "$sum": 1 }
}
},
{
"$project": {
"eventsArgs.elementId": "$_id",
"count": 1, "_id": 0
}
}
]);
The following should work as well:
db.analytics.aggregate([
{
"$match": {
"eventArgs.type": 'touchstart',
"eventType": 'mousedown',
"creationDateTime": { "$gte": ISODate("2017-02-24T000:00:00.000Z") }
}
},
{
"$group": {
"_id": {
"eventArgs": {
"elementId": "$eventArgs.elementId"
}
},
"count": { "$sum": 1 }
}
}
]);

FieldPath field names may not contain '.' in $group

I have the following mongo data which looks like this
{
eventType : "mousedown",
eventArgs : {
type : "touchstart",
elementId : "id1"
},
creationDateTime : ISODate("2017-02-24T07:05:49.986Z")
}
I wrote the following query to perform group count.
db.analytics.aggregate
(
{
$match :
{
$and :
[
{"eventArgs.type" : 'touchstart'},
{eventType : 'mousedown'},
{creationDateTime : {$gte : ISODate("2017-02-24T000:00:00.000Z")}}
]
}
},
{
$group :
{
_id :
{
"eventsArgs.elementId" : "$elementId"
},
count :
{
$sum : 1
}
}
}
);
I'm getting error for $group, which states that
FieldPath field names may not contain '.'
If I were not able to specific '.' in
$group :
{
_id :
{
"eventsArgs.elementId" : "$elementId"
},
What is the correct way to do so?
Since you have a single group field, the best way is to just use the _id group key on that field and then create another $project pipeline that will reshape the _id key from the previous pipeline into the desired subdocument that you want. For example
db.analytics.aggregate([
{
"$match": {
"eventArgs.type": 'touchstart',
"eventType": 'mousedown',
"creationDateTime": { "$gte": ISODate("2017-02-24T000:00:00.000Z") }
}
},
{
"$group": {
"_id": "$eventArgs.elementId",
"count": { "$sum": 1 }
}
},
{
"$project": {
"eventsArgs.elementId": "$_id",
"count": 1, "_id": 0
}
}
]);
The following should work as well:
db.analytics.aggregate([
{
"$match": {
"eventArgs.type": 'touchstart',
"eventType": 'mousedown',
"creationDateTime": { "$gte": ISODate("2017-02-24T000:00:00.000Z") }
}
},
{
"$group": {
"_id": {
"eventArgs": {
"elementId": "$eventArgs.elementId"
}
},
"count": { "$sum": 1 }
}
}
]);

Mongodb Aggregation Rows to Columns

I have the following dataset. I need to group them by Account, and then turn the Element_Fieldname into a column.
var collection = [
{
Account:12345,
Element_Fieldname:"cars",
Element_Value:true
},
{
Account:12345,
Element_Fieldname:"boats",
Element_Value:false
}
]
This was my attempt to convert rows to columns, but its not working.
db.getCollection('my_collection').aggregate([{
$match : {
Element_Fieldname : {
$in : ["cars", "boats"]
}
}
}, {
$group : {
_id : "$Account",
values : {
$addToSet : {
field : "$Element_Fieldname",
value : "$Element_Value"
}
}
}
}, {
$project : {
Account : "$_id",
cars : {
"$cond" : [{
$eq : ["$Element_Fieldname", "cars"]
}, "$Element_Value", null]
},
boats : {
"$cond" : [{
$eq : ["$Element_Fieldname", "day_before_water_bottles"]
}, "$Element_Value", null]
},
}
}
])
This just gives me null in my cars and boats fields. Any help would be great.
And this is my desired results:
var desiredResult = [
{
Account:12345,
cars:true,
boats:false
}
]
this is a big tricky but you will get what you need :-)
please add $match on the top of aggregation pipeline
db.collection.aggregate([{
$project : {
_id : 0,
"Account" : 1,
car : {
$cond : [{
$eq : ["$Element_Fieldname", "cars"]
}, "$Element_Value", null]
},
boats : {
$cond : [{
$eq : ["$Element_Fieldname", "boats"]
}, "$Element_Value", null]
},
}
},
{
$group : {
_id : "$Account",
carData : {
$addToSet : "$car"
},
boatsData : {
$addToSet : "$boats"
}
}
}, {
$unwind : "$carData"
}, {
$match : {
carData : {
$ne : null
}
}
}, {
$unwind : "$boatsData"
}, {
$match : {
boatsData : {
$ne : null
}
}
},
])
and result
{
"_id" : 12345,
"carData" : true,
"boatsData" : false
}
It is not possible to do the type of computation you are describing with the aggregation framework, however there is a proposed $arrayToObject expression which will give you the functionality to peek into the key names, and create new key/values dynamically.
For example, you could do
db.collection.aggregate([
{
"$match": { "Element_Fieldname":{ "$in": ["cars", "boats"] } }
},
{
"$group": {
"_id": "$Account",
"attrs": {
"$push": {
"key": "$Element_Fieldname",
"val": "$Element_Value"
}
}
}
},
{
"$project": {
"Account": "$_id",
"_id": 0,
"newAttrs": {
"$arrayToObject": {
"$map": {
"input": "$attrs",
"as": "el",
in: ["$$el.key", "$$el.val"]
}
}
}
}
},
{
"$project": {
"Account": 1,
"cars": "$newAttrs.cars",
"boats": "$newAttrs.boats"
}
}
])
Vote for this jira ticket https://jira.mongodb.org/browse/SERVER-23310 to get this feature.
As a workaround, mapreduce seems like the available option. Consider running the following map-reduce operation:
db.collection.mapReduce(
function() {
var obj = {};
obj[this.Element_Fieldname] = this.Element_Value;
emit(this.Account, obj);
},
function(key, values) {
var obj = {};
values.forEach(function(value) {
Object.keys(value).forEach(function(key) {
obj[key] = value[key];
});
});
return obj;
},
{ "out": { "inline": 1 } }
)
Result:
{
"_id" : 12345,
"value" : {
"cars" : true,
"boats" : false
}
}