I am trying to create an objects which consist of other objects. I can't figure out the syntax though. I read the Little Book on CoffeeScript but it does not say what to do in this situation. I tried the following syntaxes but can't get it right:
objectOfHomes = {
{
location: "Paris Island", age: 18
}
}
objectOfHomes:
{location: "Paris Island", age: 18},
{location: "29 Palms", age: 18},
{location: "Camp Lejeune", age: 19},
{location: "Iraq", age: 20},
{location: "Camp Lejeune", age: 20},
{location: "Mesa Verda", age: 22}
objectOfHomes =
{location: "Paris Island", age: 18}
{location: "29 Palms", age: 18}
{location: "Camp Lejeune", age: 19}
{location: "Iraq", age: 20}
{location: "Camp Lejeune", age: 20}
{location: "Mesa Verda", age: 22}
In coffeescript you could do it like this:
objectOfHomes:
a:
location: "Paris Island"
age: 18
You are likely better off using an array of objects than on object of objects, unless you have a primary key of some sort which you do not appear to have.
objectOfHomes = [
{location: "Paris Island", age: 18}
{location: "29 Palms", age: 18}
{location: "Camp Lejeune", age: 19}
{location: "Iraq", age: 20}
{location: "Camp Lejeune", age: 20}
{location: "Mesa Verda", age: 22}
]
You need to include key values with your objects:
objectOfHomes:
a: {location: "Paris Island", age: 18}
b: {location: "29 Palms", age: 18}
c: {location: "Camp Lejeune", age: 19}
d: {location: "Iraq", age: 20}
e: {location: "Camp Lejeune", age: 20}
f: {location: "Mesa Verda", age: 22}
Related
I would like to perform an aggregation query, then a find query, and apply the output of the aggregation as a new field in the find results, ie:
A have dataset like this:
{id: 1, city: "Paris", comment: "...", status: "Active"},
{id: 2, city: "London", comment: "...", status: "Active"},
{id: 3, city: "Paris", comment: "...", status: "Active"},
{id: 4, city: "New York", comment: "...", status: "Active"},
{id: 5, city: "London", comment: "...", status: "Active"},
{id: 6, city: "London", comment: "...", status: "Active"},
{id: 7, city: "London", comment: "...", status: "Disabled"}
I want to get the counts for each active city:
collection.aggregate([
{$match: {status: "Active"}},
{$group: {_id: "$city", count: {$sum: 1}}}
])
But I would like to apply the count to each entry, matched according to city. It would return something like this:
{id: 1, city: "Paris", comment: "...", status: "Active", count: 2},
{id: 2, city: "London", comment: "...", status: "Active", count: 3},
{id: 3, city: "Paris", comment: "...", status: "Active", count: 2},
{id: 4, city: "New York", comment: "...", status: "Active", count: 1},
{id: 5, city: "London", comment: "...", status: "Active", count: 3},
{id: 6, city: "London", comment: "...", status: "Active", count: 3},
{id: 7, city: "London", comment: "...", status: "Disabled", count: 3}
Ideally I would like to do this in a single query so that it can be sorted and paginated according to count.
$group by city and push root object to a root field, count status that is Active only
$unwind deconstruct root array
$mergeObjects to merge $root object and count field
$replaceRoot to replace merged object to root
db.collection.aggregate([
{
$group: {
_id: "$city",
root: { $push: "$$ROOT" },
count: {
$sum: {
$cond: [{ $eq: ["$status", "Active"] }, 1, 0]
}
}
}
},
{ $unwind: "$root" },
{
$replaceRoot: {
newRoot: { $mergeObjects: ["$root", { count: "$count" }] }
}
}
])
Playground
How to Remove a particular value for all records in the collection using aggregation :
Have a collection with data :
[
{
_id: "bmasndvhjbcw",
name: "lucas",
occupation: "scientist",
present_working:true,
age: 55,
location: "texas"
},
{
_id: "bmasndvhjbcx",
name: "mark",
occupation: "scientist",
age: 45,
present_working:false,
location: "texas"
},
{
_id: "bmasndvhjbcq",
name: "cooper",
occupation: "physicist",
age: 69,
location: "texas",
present_working:false
}
]
Remove the rows in records for which there is present_working:false. Data need not be removed in the database, it should be only modified in the aggregation pipeline
Expected output after removing only present_working:false and present_working:false should be kept in database. :
[
{
_id: "bmasndvhjbcw",
name: "lucas",
occupation: "scientist",
present_working:true,
age: 55,
location: "texas"
},
{
_id: "bmasndvhjbcx",
name: "mark",
occupation: "scientist",
age: 45,
location: "texas"
},
{
_id: "bmasndvhjbcq",
name: "cooper",
occupation: "physicist",
age: 69,
location: "texas"
}
]
MongoDB version: 4.0
You can use $$REMOVE as part of $project:
db.collection.aggregate([
{
$project: {
_id: 1,
name: 1,
occupation: 1,
age: 1,
location: 1,
present_working: { $cond: [ { $eq: [ "$present_working", false ] }, "$$REMOVE", "$present_working" ] }
}
}
])
Mongo Playground
How to Remove a particular value for all records in the collection using aggregation :
Have a collection with data :
[
{
_id: "bmasndvhjbcw",
name: "lucas",
occupation: "scientist",
present_working:true,
age: 55,
location: "texas",
date:2019-11-25T10:49:36.534+00:00
},
{
_id: "bmasndvhjbcx",
name: "mark",
occupation: "scientist",
age: 45,
present_working:true,
location: "texas",
date:null
},
{
_id: "bmasndvhjbcq",
name: "cooper",
occupation: "physicist",
age: 69,
location: "texas",
date:null
}
]
Remove the rows in records for which there is date:null. Data need not be removed in the database, it should be only modified in the aggregation pipeline
Expected output after removing only date:null :
[
{
_id: "bmasndvhjbcw",
name: "lucas",
occupation: "scientist",
present_working:true,
age: 55,
location: "texas",
date:2019-11-25T10:49:36.534+00:00
},
{
_id: "bmasndvhjbcx",
name: "mark",
occupation: "scientist",
age: 45,
present_working:true,
location: "texas"
},
{
_id: "bmasndvhjbcq",
name: "cooper",
occupation: "physicist",
age: 69,
location: "texas"
}
]
MongoDB version: 4.0
You can use $ifNull operator & $$REMOVE to do that :
db.collection.aggregate([
{
$addFields: {
date: {
$ifNull: [
"$date",
"$$REMOVE"
]
}
}
}
])
Test : MongoDB-Playground
Have a collection with records , Need to convert boolean values of a column to string :
[
{
_id: "bmasndvhjbcw",
name: "lucas",
occupation: "scientist",
passed_phd: true,
age: 55,
location: "texas",
},
{
_id: "bmasndvhjbcx",
name: "mark",
occupation: "scientist",
age: 45,
passed_phd: true,
location: "texas",
},
{
_id: "bmasndvhjbca",
name: "stuart",
occupation: "lab assistant",
age: 25,
passed_phd: false,
location: "texas",
},
{
_id: "bmasndvhjbcq",
name: "cooper",
occupation: "physicist",
age: 69,
passed_phd: false,
location: "texas"
}
]
how to change boolean value of the records to string.
value that has true(boolean) in passed_phd should be converted to "yes"(string)
value that has false(boolean) in passed_phd should be converted to "no"(string)
[
{
_id: "bmasndvhjbcw",
name: "lucas",
occupation: "scientist",
passed_phd: "yes",
age: 55,
location: "texas",
},
{
_id: "bmasndvhjbcx",
name: "mark",
occupation: "scientist",
age: 45,
passed_phd: "yes",
location: "texas",
},
{
_id: "bmasndvhjbca",
name: "stuart",
occupation: "lab assistant",
age: 25,
passed_phd: "no",
location: "texas",
},
{
_id: "bmasndvhjbcq",
name: "cooper",
occupation: "physicist",
age: 69,
passed_phd: "no",
location: "texas"
}
]
mongodb version 4.0
.
The $cond aggregation pipeline operator can do this.
Beginning with MongoDB 4.2, these can be used in an update or in an aggregation pipeline, like:
db.collection.aggregate([
{$set:{
passed_phd:{
$cond:{
if:"$passed_phd",
then:"yes",
else:"no"
}
}
}}
])
Hello everyone I want to group mongodb query results as;
var obj = {dynamicKey: value}
For example collection is like;
{name: "alex" , age: 21},
{name: "felix" , age: 81},
{name: "hannah" , age: 12},
{name: "tom" , age: 18}
I want to get the collection as;
{alex: 21},
{felix: 81},
{hannah: 12},
{tom: 18}
Is it possible to do this with $group in mongodb?
db.collection.aggregate([
{
$group : {
_id: "",
"$name" : { $first: "$age"}
}
}
]).pretty()
But this throws an error as; "errmsg" : "The field name '$name' cannot be an operator name",
How can I do this?