I have a document with the following structure:
id: 6229d3fd40bac327243e4ww2
userID: "594e710c-c632-3ae-929a-f13e294ee6ef"
appID: "622994e2b98225609b89affd"
notifications:Object
6229d3e040bac3272qwe4441: 1
622ew3e041bac3272qwe4441: 1
I want to update this structure to this:
id: 6229d3fd40bac327243e4ww2
userID: "594e710c-c632-43ae-929a-f13e294ee6ef"
appID: "622994e2b98225609b89affd"
notifications:Object
6229d3e040bac3272qwe4441: {a:1, b:false}
622ew3e040bac3272qwe4441: {a:1, b:false}
I am having trouble with iterating the notification objects fields as its is random hex string.
I have tried using following solutions but not able to get the expected result
MongoDB Aggregation pipeline: use of For loop
db.collection.update({},
[
{
$set: {
notifications: {
$arrayToObject: {
$map: {
input: { $objectToArray: "$notifications" },
as: "n",
in: {
k: "$$n.k",
v: { a: "$$n.v", b: false }
}
}
}
}
}
}
],
{
multi: true
})
mongoplayground
Related
I have a database of a the employees of a company that looks like this:
{
_id: 7698,
name: 'Blake',
job: 'manager',
manager: 7839,
hired: ISODate("1981-05-01T00:00:00.000Z"),
salary: 2850,
department: {name: 'Sales', location: 'Chicago'},
missions: [
{company: 'Mac Donald', location: 'Chicago'},
{company: 'IBM', location: 'Chicago'}
]
}
I have an exercise in which I need to write the MongoDb command that returns all them employees who did all their missions in Chicago. I struggle with the all because I cannot find a way to check that all the locations of the missions array are equal to 'Chicago'.
I was thinking about doing it in two time: first find the total number of missions the employee has and then compare it to the number of mission he has in Chicago (that how I would do in SQL I guess). But I cannot found the number of mission the employee did in Chicago. Here is what I tried:
db.employees.aggregate([
{
$match: { "missions": { $exists: true } }
},
{
$project: {
name: 1,
nbMissionsChicago: {
$sum: {
$cond: [
{
$eq: [{
$getField: {
field: { $literal: "$location" },
input: "$missions"
}
}, "Chicago"]
}, 1, 0
]
}
}
}
}
])
Here is the result :
{ _id: 7698, name: 'Blake', nbMissionsChicago: 0 }
{ _id: 7782, name: 'Clark', nbMissionsChicago: 0 }
{ _id: 8000, name: 'Smith', nbMissionsChicago: 0 }
{ _id: 7902, name: 'Ford', nbMissionsChicago: 0 }
{ _id: 7499, name: 'Allen', nbMissionsChicago: 0 }
{ _id: 7654, name: 'Martin', nbMissionsChicago: 0 }
{ _id: 7900, name: 'James', nbMissionsChicago: 0 }
{ _id: 7369, name: 'Smith', nbMissionsChicago: 0 }
First of all, is there a better method to check that all the locations of the missions array respect the condition? And why does this commands returns only 0 ?
Thanks!
If all you need is the agents who had all their missions in "Chicago" then you don't need an aggregation pipeline for it, specifically the approach of filtering the array as part of the aggregation can't utilize an index and will make performance even worse.
A simple query should suffice here:
db.collection.find({
$and: [
{
"missions": {
$exists: true
}
},
{
"missions.location": {
$not: {
$gt: "Chicago"
}
}
},
{
"missions.location": {
$not: {
$lt: "Chicago"
}
}
}
]
})
Mongo Playground
This way we can build an index on the missions field and utilize it properly, any documents with a different value other then "Chigaco" will not match as they will fail the $gt or $lt comparion.
Note that an empty array also matches the condition, you can change the generic "missions" exists condition key into "missions.0": {$exists: true}, this will also require at least one mission.
You are unable to get the correct result as it is not the correct way to iterate the element in an array field.
Instead, you need to work with $size operator to get the size of an array and the $filter operator to filter the document.
Updated: You can directly compare the filtered array with the original array.
db.employees.aggregate([
{
$match: {
"missions": {
$exists: true
}
}
},
{
$project: {
name: 1,
nbMissionsChicago: {
$eq: [
{
$filter: {
input: "$missions",
cond: {
$eq: [
"$$this.location",
"Chicago"
]
}
}
},
"$missions"
]
}
}
}
])
Demo # Mongo Playground
My model :
const scheduleTaskSchema = new Schema({
activity: { type: Object, required: true },
date: { type: Date, required: true },
crew: Object,
vehicle: Object,
pickups: Array,
details: String,
});
const ScheduleTaskModel = mongoose.model("schedule_task", scheduleTaskSchema),
and this aggregation pipeline :
let aggregation = [
{
$sort: {
"pickups.0.time": 1,
},
},
{
$group: {
_id: "$date",
tasks: { $push: "$$ROOT" },
},
},
{ $sort: { _id: -1 } },
];
if (hasDateQuery) {
aggregation.unshift({
$match: {
date: { $gte: new Date(start_date), $lte: new Date(end_date) },
},
});
} else {
aggregation.push({ $limit: 2 });
}
const scheduledTasksGroups = await ScheduleTaskModel.aggregate(aggregation);
the crew object can have arbitrary number of keys with this structure :
crew : {
drivers: [
{
_id: "656b1e9cf5b894a4f2v643bc",
name: "john"
},
{
_id: "567b1e9cf5b954a4f2c643bhh",
name: "bill"
}
],
officers: [
{
_id: "655b1e9cf5b6632a4f2c643jk",
name: "mark"
},
{
_id: "876b1e9af5b664a4f2c234bb",
name: "jane"
}
],
//...any number of keys that contain an array of objects that all have an _id
}
I'm looking for a way to return all documents (before sorting/grouping) that contain a given _id anywhere within the crew object without knowing which key to search,it can be many different keys that all contain an array of objects that all have an _id
Any ideas ?
You can use $objectToArray for this:
db.collection.aggregate([
{$addFields: {crewFilter: {$objectToArray: "$crew"}}},
{$set: {
crewFilter: {$size: {
$reduce: {
input: "$crewFilter",
initialValue: [],
in: {$concatArrays: [
"$$value",
{$filter: {
input: "$$this.v",
as: "member",
cond: {$eq: ["$$member._id", _id]}
}
}
]
}
}
}}
}},
{$match: {crewFilter: {$gt: 0}}}
])
See how it works on the playground example
I have following data in my collection:
[
{
_id: "2313123123",
metadata: {
path: "...",
value: "...",
name: "..."
}
},
{
_id: "2313123123",
metadata: {
path: "...",
name: "...",
origin: "...",
}
},
{
_id: "2313123123",
metadata: {
path: "...",
source: "..."
}
},
]
I want to retrieve all distinct key names of the field metadata from my documents.
I want to retrieve ["path", "value", "name", "origin", "source"].
How can I query for this? Is this possible with the distinct method or do I need to use aggregate?
You'll have to use an aggregate for this, sadly due to the nature of your needs this is going to be a very "expensive" pipeline to execute. There is no way to avoid iterating over the entire collection and adding the unique keys to the array.
We're going to use $objectToArray to turn metadata into an array, then $unwind it and finally using $group we could save all the unique values.
db.collection.aggregate([
{
$project: {
keys: {
$map: {
input: {
"$objectToArray": "$metadata"
},
in: "$$this.k"
}
}
}
},
{
$unwind: "$keys"
},
{
$group: {
_id: null,
keys: {
"$addToSet": "$keys"
}
}
}
])
Mongo Playground
db.collection.aggregate([
{
$addFields: {
metadata: {
$objectToArray: "$metadata"
}
}
},
{
$unwind: "$metadata"
},
{
$group: {
_id: "distinct",
dist: {
$addToSet: "$metadata.k"
}
}
}
])
explained:
Convert the metadata object to metadata array having the keys as values in k key.
Unwind the metadata array of k keys & v values
group with addToSet to extract only the distinct k values in the final result.
playground
helpfull javascript onliner from mongo shell option:
db.collection.find({},{metadata:1,_id:0}).forEach( function(doc) { for (key in doc.metadata) s.push(key); } );uni = Array.from(new Set(s));printjson(uni);
["path","name","origin","source","value"]
How to remove an empty array from JSON in MongoDB.I have multiple arrays in the object.
db.runCommand({
update: "table",
updates: [
{
q: {_id: { $in: ['id1', 'id2']}},
u: {
$pull: { "a.b" : { "a.$[].b.$[].c" : { $exists: true, $size: 0 } }}
}
}
]
})
It only tell about empty array,but does not modify it.
you are almost there but instead of $pull, you need to use $unset, as pull just remove the data from an array but unset will delete an array itself.
db.runCommand({
update: "table",
updates: [
{
q: {_id: { $in: ['id1', 'id2']}},
u: {
$unset: { "a.b" : { "a.$[].b.$[].c" : { $exists: true, $size: 0 } }}
}
}
]
})
I have an object that has an array of page objects and each page object has an array of questions.
Ex object:
{
Id: 1,
UserId: 14,
Deleted: false,
Collaborators: [],
Title: "Awesome",
Pages: [{
Id: 1,
Title: 'Jank',
Questions: [
{ Id: 1, Content: 'Ask me about it' },
{ Id: 2, Content: 'Ask me about it again' }
]
}, {
Id: 2,
Title: 'Janker',
Questions: [
{ Id: 1, Content: 'Tell me about it' },
{ Id: 2, Content: 'Tell me about it again' }
]
}]
}
What I am trying to do is to get a count of all the questions for the entire bas object. I am not sure how to do that. I have tried to use aggregate and $sum the total questions and then do another function to $sum those all together to get a total for the entire object. Unfortunately my $sum is not working like I thought it would.
Ex code (nodejs):
var getQuestionCount = function(id) {
var cursor = mongo.collection('surveys').aggregate([{
$match: {
$or: [{
"UserId": id
}, {
"Collaborators": {
$in: [id]
}
}]
}
}, {
$match: {
"Deleted": false
}
}, {
$unwind: "$Pages"
},
{ $group: { _id: null, number: { $sum: "$Pages.Questions" } } }
], function(err, result) {
//This log just gives me [object Object], [object Object]
console.log('q count ' + result);
});
}
Any idea how to do this? My end result from the example object above would ideally return 4 as the question count for the whole object.
I'd try following shell query.
db.collection.aggregate([
// filter out unwanted documents.
{$match:{Id: 1}},
// Unwind Pages collection to access Questions array
{$unwind:"$Pages"},
// Count items in Questions array
{$project:{count: {$size:"$Pages.Questions"}}},
// Finally sum items previously counted.
{$group:{_id:"$_id", total: {$sum: "$count"}}}
])
Based on your sample document, it should return correct count of Questions.
{
"_id" : ObjectId("57723bb8c10c41c41ff4897c"),
"total" : NumberInt(4)
}