Search by Date within an Object in Array - node.js

I have the following Mongoose schema:
let ExerciserSchema = new Schema({
username: {
type: String,
required: true
},
exercises: [{
desc: String,
duration: Number,
date: {
type: Date,
default: new Date()
}
}]
});
I want to search by username and limit the exercise results to a date range.
I tried this lookup function:
let user = await Exerciser.find(
{ "username": name },
{ "exercises.date": { "$gte": from }},
{ "exercises.date": { "$lte": to }}
).exec((err, data) => {
if (err) {
res.json({ Error: "Data not found" })
return done(err);
}
else {
res.json(data);
return done(null, data);
}
});
However, it's logging an error and not returning the data.
MongoError: Unsupported projection option: exercises.date: { $gte: new Date(1526342400000) }
I realize from that error it appears like my date is being searched for in milliseconds, but I console.log it right before I run the above function and it's in date mode, which is what I think I want: 2018-05-01T00:00:00.000Z
How can I make this work so that I can search by a date range given my Schema? I can change the format of the date in the Schema if necessary. I'd just like the simplest solution. Thanks for your help.

You're query is wrong. You were trying to write an AND condition, but you separated documents instead of putting everything into one. This means the "second" argument to Model.find() was interpreted as a a "projection of fields", hence the error:
MongoError: Unsupported projection option:
So it's not a "schema problem" but that you sent the wrong arguments to the Model.find() method
Also you need $elemMatch for multiple conditions on elements within an array:
// Use a try..catch block with async/await of Promises
try {
let user = await Exerciser.find({
"username": name,
"exercises": {
"$elemMatch": { "date": { "$gte": from, "$lte": to } }
}
});
// work with user
} catch(e) {
// handle any errors
}
Most importantly you don't await a callback. You either await the Promise like I am showing here or simply pass in the callback instead. Not both.
Exerciser.find({
"username": name,
"exercises": {
"$elemMatch": { "date": { "$gte": from, "$lte": to } }
}
}).exec((err,user) => {
// the rest
})
FYI, what you were attempting to do was this:
Exerciser.find({
"$and": [
{ "username": name },
{ "exercises.date": { "$gte": from }},
{ "exercises.date": { "$lte": to }}
]
)
But that is actually still incorrect since without the $elemMatch the $gte and $lte applies to ALL elements of the array and not just a single one. So the incorrect results would show if ANY array item was less than the date but not necessarily greater than.
For array elements the $elemMatch enforces the "between" of the two conditions.

I managed to get it. This answer matches by username, and filters exercises so they are between the dates with variable names to and from. This is what I had wanted.
let user = Exerciser.aggregate([
{ $match: { "username": id }},
{ $project: { // $project passes along the documents with the requested fields to the next stage in the pipeline
exercises: { $filter: {
input: "$exercises",
as: "exercise",
cond: { $and: [
{ $lte: [ "$$exercise.date", to ] },
{ $gte: [ "$$exercise.date", from ] },
]}
}},
username: 1, // include username in returned data
_id: 0
}}
])
Result:
[
{
"username": "scott",
"exercises": [
{
"desc": "Situps",
"duration": 5,
"_id": "5af4790fd9a9c80c11aac696",
"date": "2018-04-30T00:00:00.000Z"
},
{
"desc": "Situps",
"duration": 10,
"_id": "5afb3f03e12e38020d059e67",
"date": "2018-05-01T00:00:00.000Z"
},
{
"desc": "Pushups",
"duration": 8,
"_id": "5afc08aa9259ed008e7e0895",
"date": "2018-05-02T00:00:00.000Z"
}
]
}
]

Related

Mongoose Aggregate match if an Array contains any value of another Array

I am trying to match by comparing the values inside the values of another array, and return it if any one of the values in the 1st array match any one value of the 2nd array. I have a user profile like this
{
"user": "bob"
"hobbies": [jogging]
},
{
"user": "bill"
"hobbies": [reading, drawing]
},
{
"user": "thomas"
"hobbies": [reading, cooking]
}
{
"user": "susan"
"hobbies": [coding, piano]
}
My mongoose search query as an example is this array [coding, reading] (but could include any hobby value) and i would like to have this as an output:
{
"user": "bill"
"hobbies": [reading, drawing]
},
{
"user": "thomas"
"hobbies": [reading, cooking]
}
{
"user": "susan"
"hobbies": [coding, piano]
}
I tried:
{"$match": {"$expr": {"$in": [searchArray.toString(), "$hobbies"]}}}
but this only works aslong as the search array has only one value in it.
const searchArray = ["reading", "coding"];
const orArray = searchArray.map((seachValue) => {
return {
hobies: searchValue,
}
});
collection.aggregate([{
$match: { $or: orArray }
}])
The query:
db.collection.aggregate([
{
$match: {
"$or": [
{
hobbies: "reading"
},
{
hobbies: "coding"
}
]
}
}
])
Or you can use another way, no need to handle arr:
db.collection.aggregate([
{
$match: {
hobbies: {
$in: [
"reading",
"coding"
]
}
}
}
])
Run here

findOne() returns entire document, instead of a single object

I'm trying to query this set of data using findOne():
{
"_id": {
"$oid": "5c1a4ba1482bf501ed20ae4b"
},
"wardrobe": {
"items": [
{
"type": "T-shirt",
"colour": "Gray",
"material": "Wool",
"brand": "Filson",
"_id": "5c1a4b7d482bf501ed20ae4a"
},
{
"type": "T-shirt",
"colour": "White",
"material": "Acrylic",
"brand": "H&M",
"_id": "5c1a4b7d482bf501ed20ae4a"
}
]
},
"tokens": [],
"email": "another#new.email",
"password": "$2a$10$quEXGjbEMX.3ERdjPabIIuMIKu3zngHDl26tgRcCiIDBItSnC5jda",
"createdAt": {
"$date": "2018-12-19T13:46:09.365Z"
},
"updatedAt": {
"$date": "2018-12-19T13:47:30.123Z"
},
"__v": 2
}
I want to return a single object from the items array using _Id as a filter. This is how I'm doing that:
exports.deleteItem = (req, res, next) => {
User.findOne({ 'wardrobe.items': { $elemMatch: { "_id": "5c1a4b7d482bf501ed20ae4a",} } }, (err, item) => {
console.log(item);
if (err) {
return console.log("error: " + err);
}
res.redirect('/wardrobe');
});
};
However, console.log(item) returns the whole document—like so:
{ wardrobe: { items: [ [Object], [Object] ] },
tokens: [],
_id: 5c1a4ba1482bf501ed20ae4b,
email: 'another#new.email',
password:
'$2a$10$quEXGjbEMX.3ERdjPabIIuMIKu3zngHDl26tgRcCiIDBItSnC5jda',
createdAt: 2018-12-19T13:46:09.365Z,
updatedAt: 2018-12-19T13:47:30.123Z,
__v: 2 }
I want to eventually use this to delete single items, so I need to filter to the single object from the subdocument.
Concerning your question:
MongoDB always returns the full object matching your query, unless you add a projection specifying which fields should be returned.
If you really want to only return a nested object, you could use the aggregation pipeline with the $replaceRoot operator like this:
User.aggregate([
// you can directly query for array fields instead of $elemMatching them
{ $match: { 'wardrobe.items._id': "5c1a4b7d482bf501ed20ae4a"}}},
// this "lifts" the fields wardrobe up and makes it the new root
{ $replaceRoot: {newRoot: '$wardrobe'}
// this "splits" the array into separate objects
{ $unwind: '$items'},
// this'll remove all unwanted elements
{ $match: { 'items._id': "5c1a4b7d482bf501ed20ae4a" },
},
])
This should return only the wanted items.
A note though: If you plan to remove elements from arrays anyways, I'd rather suggest you have a look at the $pull operation, which can remove an element from an array if it matches a certain condition:
https://docs.mongodb.com/manual/reference/operator/update/pull/
User.update(
{ 'wardrobe.items._id': "5c1a4b7d482bf501ed20ae4a"},
{ $pull: { 'wardrobe.items': {_id: "5c1a4b7d482bf501ed20ae4a"}},
{ multi: true }
)

Mongoose/mongodb - get only latest records per id

I have an Inspection model in mongoose:
var InspectionSchema = new Schema({
business_id: {
type: String,
required: true
},
score: {
type: Number,
min: 0,
max: 100,
required: true
},
date: {
type: Number, // in format YYYYMMDD
required: true
},
description: String,
type: String
});
InspectionSchema.index({business_id: 1, date: 1}, {unique: true});
It is possible for there to be multiple inspections on the same Business (each Business is represented by a unique business_id). However, there is a limit of one inspection per business per day, which is why there is a unique index on business_id + date.
I also created a static method on the Inspection object which, given a list of business_ids, retrieves all of the inspections for the underlying businesses.
InspectionSchema.statics.getAllForBusinessIds = function(ids, callback) {
this.find({'business_id': {$in: ids}}, callback);
};
This function fetches all of the inspections for the requested businesses. However, I want to also create a function that fetches only the latest inspection per business_id.
InspectionSchema.statics.getLatestForBusinessIds = function(ids, callback) {
// query to get only the latest inspection per business_id in "ids"?
};
How might I go about implementing this?
You can use the .aggregate() method in order to get all the latest data in one request:
Inspection.aggregate(
[
{ "$sort": { "buiness_id": 1, "date": -1 } },
{ "$group": {
"_id": "$business_id",
"score": { "$first": "$score" },
"date": { "$first": "$date" },
"description": { "$first": "$description" },
"type": { "$first": "$type" }
}}
],
function(err,result) {
}
);
Just $sort then $group with the "business_id" as the grouping key. The $first gets the first results from the grouping boundary, where we already sorted by date within each id.
If you just want the date then do this using $max:
Inspection.aggregate(
[
{ "$group": {
"_id": "$business_id",
"date": { "$max": "$date" }
}}
],
function(err,result) {
}
);
Also see $match if you want to "pre-filter" the business id values or any other conditions when doing this.
try this:
Inpection.aggregate(
[
{ $match : { _id : { "$in" : ids} } },
{ $group: { "_id" : "$business_id", lastInspectionDate: { $last: "$date" } } }
],
function(err,result) {
}
);

Mongoose error on aggregate group

i have this model:
var Chat = new Schema({
from: String,
to: String,
satopId: String,
createdAt: Date
});
var Chat = mongoose.model('Chat', Chat);
I want do a query to do a query that returns the max created at grouping by to and from field. I tried with:
Chat.aggregate([
{
$group: {
_id: '$to',
from: '$from',
createdAt: {
$max: '$createdAt'
}
}
},
{
$project: {
_id: 1,
createdAt: 1,
from: 1,
to: 1
}
}
], function(err, docs){
})
But this generates this error:
the group aggregate field 'from' must be defined as an expression
inside an object
I don't understand what does it mean. How can i solve it?
Thanks
Anything "outside" if the _id expression in a $group statement requires a "grouping operator" of some sort.
Assuming you are happy with the idea of the "sole" grouping key to be the "to" field in your documents then you probably want something like $first:
Chat.aggregate([
{ "$group": {
"_id": "$to",
"from": { "$first": "$from" },
"createdAt": { "$max": "$createdAt" }
}},
function (err, docs) {
// do something here
}
])
Otherwise if you want "distinct" values on "both" fields then they both belong as a composite value of the grouping key:
Chat.aggregate([
{ "$group": {
"_id": {
"to": "$to",
"from": "$from"
},
"createdAt": { "$max": "$createdAt" }
}},
function (err, docs) {
// do something here
}
])
That's basically how it works. Either it's part of the key or something that needs a grouping operator just like with SQL.
Also note that your $project assertion is not really required as $group already does what you are asking there.

node.js/mongoDB: Return custom value if field is null

What I want to do is to query my database for documents and, if a field is not set, return a custom value instead of it.
Imagine I'm having a collection of songs and some of these have the field pathToCover set and some don't. For these, I want to return the URL to a placeholder image that I store in a config variable.
I am running node.js and mongoDB with express and mongoose.
I am not sure what the best approach for this is. An obvious solution would be to query for the documents and then in the callback iterate over them to check if the field is set. But this feels quite superfluous.
Currently, my code looks like this:
exports.getByAlbum = function listByAlbum(query, callback) {
Song.aggregate({
$group: {
_id: '$album',
artists: { $addToSet: '$artist' },
songs: { $push: '$title' },
covers: { $addToSet: '$pathToCover'},
genres: { $addToSet: '$genre'},
pathToCover: { $first: '$pathToCover'}
}
},
function (err, result) {
if (err) return handleError(err);
result.forEach(function(album) {
if ( album.pathToCover == null) {
album.pathToCover = config.library.placeholders.get('album');
}
})
callback(result);
});
}
What is the best approach for this?
Thanks a lot in advance!
Where the value of the field is either null or is unset and possibly missing from the document, then you use $ifNull in your aggregation to set to the alternate value:
exports.getByAlbum = function listByAlbum(query, callback) {
var defaultCover = config.library.placeholders.get('album');
Song.aggregate(
[
{ "$group": {
"_id": "$album",
"artists": { "$addToSet": "$artist" },
"songs": { "$push": "$title" },
"covers": { "$addToSet": { "$ifNull": [ "$pathToCover", defaultCover ] } },
"genres": { "$addToSet": "$genre" },
"pathToCover": { "$first": { "$ifNull": [ "$pathToCover", defaultCover ] } }
}}
],
function (err, result) {
if (err) return handleError(err);
callback(result);
}
);
}
If it is an empty string then use the $cond statement with an $eq test in place of $ifNull:
{ "$cond": [ { "$eq": [ "$pathToCover", "" ] }, defaultCover, "$pathToCover" ] }
Either statement can be used inside of a grouping operator to replace the value that is considered.
If you are just worried that perhaps not "all" of the values are set on the song, then use something like $min or $max as a appropriate to your data to just pick one of the values instead of using $first.

Resources