I have a model like this :
var field = {
createdAt: {type: Date, default: Date.now()},
updatedAt: {type: Date, default: Date.now()},
pic : {type: String, default: 'http://lorempixel.com/400/400/abstract/', writable: true},
thumb : {type: String, default: 'http://lorempixel.com/100/100/abstract/', writable: true},
name: {type: String},
description: {type: String},
isPublic: {type: Boolean, default: true},
members: [{type: Schema.Types.ObjectId, ref: 'Member'}],
}
Im using this code below to get the total count of the Member's ID in members field.
Group.aggregate([
{$match: {_id:req.params.group_id}},
{$group: {
_id: '$members',
count: {$sum: 1}
}}
], function (err, count) {
res.send(count);
});
But it returns and empty array [] how do I get the proper count?
Thanks.
Mongoose does not "autocast" to OjectId or any other schema type within the aggregation pipeline. So your operation returns nothing since it matches nothing.
Also, that would not be the approach to counting members of an array.
Better to use the $size option instead to count array members per document:
Group.aggregate([
{ "$match": { _id: ObjectId(req.params.group_id) }},
{ "$group": {
"_id": null,
"count": { "$sum": { "$size": "$members" } }
}}
], function (err, count) {
res.send(count);
});
And $group with the _id for the appropriate roll-up, or null as used here for all items in the collection.
If you wanted the "count of each distinct member" then you would process with $unwind across document(s) instead:
Group.aggregate([
{ "$match": { _id: ObjectId(req.params.group_id) }},
{ "$unwind": "$members" },
{ "$group": {
"_id": "$members",
"count": { "$sum": 1 }
}}
], function (err, count) {
res.send(count);
});
Related
I have a Schema of Project that looks like this:
const ProjectSchema = new mongoose.Schema({
name: {
type: String,
Required: true,
trim: true
},
description: {
type: String,
},
devices: [{
name: {type: String, Required: true},
number: {type: String, trim: true},
deck: {type: String},
room: {type: String},
frame: {type: String}
}],
cables: {
type: Array
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
adminsID: {
type: Array
},
createdAt: {
type: Date,
default: Date.now
}
I want to query an object from array of "devices".
I was able to add, delete and display all sub-documents from this array but I found it really difficult to get single object that matches _id criteria in the array.
The closest I got is this (I'm requesting: '/:id/:deviceID/edit' where ":id" is Project ObjectId.
let device = await Project.find("devices._id": req.params.deviceID).lean()
console.log(device)
which provides me with below info:
[
{
_id: 6009cfb3728ec23034187d3b,
cables: [],
adminsID: [],
name: 'Test project',
description: 'Test project description',
user: 5fff69af08fc5e47a0ce7944,
devices: [ [Object], [Object] ],
createdAt: 2021-01-21T19:02:11.352Z,
__v: 0
}
]
I know this might be really trivial problem, but I have tested for different solutions and nothing seemed to work with me. Thanks for understanding
This is how you can filter only single object from the devices array:
Project.find({"devices._id":req.params.deviceID },{ name:1, devices: { $elemMatch:{ _id:req.params.deviceID } }})
You can use $elemMatch into projection or query stage into find, whatever you want it works:
db.collection.find({
"id": 1,
"devices": { "$elemMatch": { "id": 1 } }
},{
"devices.$": 1
})
or
db.collection.find({
"id": 1
},
{
"devices": { "$elemMatch": { "id": 1 } }
})
Examples here and here
Using mongoose is the same query.
yourModel.findOne({
"id": req.params.id
},
{
"devices": { "$elemMatch": { "id": req.params.deviceID } }
}).then(result => {
console.log("result = ",result.name)
}).catch(e => {
// error
})
You'll need to use aggregate if you wish to get the device alone. This will return an array
Project.aggregate([
{ "$unwind": "$devices" },
{ "$match": { "devices._id": req.params.deviceID } },
{
"$project": {
name: "$devices.name",
// Other fields
}
}
])
You either await this or use .then() at the end.
Or you could use findOne() which will give you the Project + devices with only a single element
Or find, which will give you an array of object with the _id of the project and a single element in devices
Project.findOne({"devices._id": req.params.deviceID}, 'devices.$'})
.then(project => {
console.log(project.devices[0])
})
For now I worked it around with:
let project = await Project.findById(req.params.id).lean()
let device = project.devices.find( _id => req.params.deviceID)
It provides me with what I wanted but I as you can see I request whole project. Hopefuly it won't give me any long lasting troubles in the future.
I am struggling to understand the populate method in Mongoose, while having trouble populating fields from one Mongoose model to another. The first model Schema is:
var MprnSchema = new mongoose.Schema({
mprNo: {type: Number, unique: true, required: true},
siteName: String,
buildingNo: Number,
streetAddress: String,
secondAddress: String,
townCity: String,
postCode: String,
supplier: String,
siteContactName: String,
siteContactNo: String,
});
module.exports = mongoose.model("Mprn", MprnSchema);
And another Schema is:
var FaultSchema = new mongoose.Schema({
jobRef: Number,
mprNo: Number,
requestedDate: {type: Date, default: Date.now},
attendedDate: {type: Date, default: null},
siteDetails: {type: mongoose.Schema.Types.ObjectId, ref: 'Mprn'},
faultIssue: String,
});
However, when I try to populate the 'siteDetails' with the contents of Mprn, I get [] when using thhe code below:
Fault.find(reportQuery).populate('siteDetails', 'siteName
postCode').exec((err, faultResults) => {
if(err){
req.flash("error", err.message);
res.redirect("/");
}
console.log(faultResults);
res.render("reportResults", { queryResults: faultResults });
});
The object passed to be rendered (queryResults) only contains the Fault data and NOT the 'siteDetails' that should be populated. I think the problem may be when I am using the Fault.Create() method in the first place, should I be saving a reference to the MPRN model at this point, so that the there is an Id to be referenced when populating?
My samples collections are:
Fault
{
"jobRef": 60000,
"mprNo": 123456,
"faultIssue": "Test"
}
Mprn
{
"mprNo": 123456,
"siteName": "Smithson Gates",
"buildingNo": 76,
"streetAddress": "Garden Place",
"secondAddress": "Greater Manchester",
"townCity": "Salford",
"postCode": "M5 3AP",
"supplier": "test",
"siteContactName": "Mr B Jones",
"siteContactNo": "0161 000 0000"
}
Well you are using latest version of mongodb... So, You can try below $lookup aggregation... and the plus point of using this is you don't need to store _id of Mprn collection in the Fault... You can easily join both the collections with mprn number
Fault.aggregate([
{ "$match": reportQuery },
{ "$lookup": {
"from": Mprn.collection.name,
"let": { "mprNo": "$mprNo" },
"pipeline": [
{ "$match": { "$expr": { "$eq": [ "$mprNo", "$$mprNo" ] } } }
],
"as": "siteDetails"
}},
{ "$unwind": "$siteDetails" }
])
I have 2 schemas TravelRoute & Flights. I am trying to find the $min of Flight.fare.total_price and return that result with some details from TravelRoute which is ref-ed in the Flights schema. I am using Mongo 3.4.1 and Mongoose 4.8.5
const TravelRoute = new mongoose.Schema({
departureAirport: {
type: String,
required: true,
trim: true,
},
arrivalAirport: {
type: String,
required: true,
trim: true,
},
durationDays: {
type: Number
},
fromNow: {
type: Number
},
isActive: {
type: Boolean
},
class: {
type: String,
enum: ['economy', 'business', 'first', 'any']
}
}
const Flights = new mongoose.Schema({
route: {
type: mongoose.Schema.Types.ObjectId, ref: 'TravelRoute'
},
departure_date: {
type: Date
},
return_date: {
type: Date
},
fare: {
total_price: {
type: Number
}
}
}
I have the following code:
Flights.aggregate(
{$group: {
_id: {
route: '$route',
departure_date: '$departure_date',
return_date: '$return_date'
},
total_price: {$min: '$fare.total_price'}
}
},
{$lookup: {
from: 'TravelRoute',
localField: '_id.route',
foreignField: '_id',
as: 'routes'
}},
{$unwind: '$routes'},
{$project: {
'routes.departureAirport': 1,
'routes.destinationAirport': 1,
'departure_date': 1,
'return_date': 1,
'total_price': 1,
'_id': 1
}},
{$sort: {'total_price': 1}},
function(err, cheapFlights){
if (err) {
log.error(err)
return next(new errors.InvalidContentError(err.errors.name.message))
}
res.send(cheapFlights)
next()
})
The above returns an empty array [] when I use $unwind. When I comment out the $unwind stage, it returns the following:
[
{
"_id": {
"route": "58b30cac0efb1c7ebcd14e0a",
"departure_date": "2017-04-03T00:00:00.000Z",
"return_date": "2017-04-08T00:00:00.000Z"
},
"total_price": 385.6,
"routes": []
},
{
"_id": {
"route": "58ae47ddc30b175150d94eef",
"departure_date": "2017-04-03T00:00:00.000Z",
"return_date": "2017-04-10T00:00:00.000Z"
},
"total_price": 823.68,
"routes": []
}
...
I'm reasonably new to Mongo and Mongoose. I am confounded by the the fact that it returns "routes" even though I don't project that. And it doesn't return departure_date (etc) even though I ask for it? I am not sure I need $unwind as there will only be one TravelRoute per Flight.
Thanks...
Edit
Here was the final solution:
{$lookup: {
from: 'travelroutes', //<-- this is the collection name in the DB
localField: '_id.route',
foreignField: '_id',
as: 'routes'
}},
{$unwind: '$routes'},
{$project: {
departureAirport: '$routes.departureAirport',
arrivalAirport: '$routes.arrivalAirport',
departureDate: '$_id.departure_date',
returnDate: '$_id.return_date',
'total_price': 1,
'routeID': '$_id.route',
'_id': 0
}},
{$sort: {'total_price': 1}},
I am trying to remove all products from the products array where the product has no rates. In my queries below I tried to check the length of the rates array, but
none of them seem to work. Any help is appreciated.
Thanks in advance
var ProductRateSchema = new Schema({
product: {
type: Schema.ObjectId,
ref: 'products'
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
rates: [{
type: Schema.ObjectId,
ref: 'rates'
}]
});
var InventorySchema = new Schema({
name: {
type: String,
default: '',
required: 'Please enter in a name',
trim: true
},
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
products: [productRateSchema]
});
var inventoryId = req.body.inventoryId;
var productId = req.body.productId;
// none of these queries work
db.inventory.findOneAndUpdate({ '_id': inventoryId,
{$pull: {'products': { product: productId, 'rates': { $eq:[] }}}}, function (err, result) {
});
db.inventory.findOneAndUpdate({ '_id': inventoryId,
{$pull: {'products': { product: productId, 'rates': {$size: {$lt: 1}}}}}, function (err, result) {
});
db.inventory.findOneAndUpdate({ '_id': inventoryId,
{$pull: {'products': { product: productId, 'rates': null }}}, function (err, result) {
});
Don't know what you tried since it is simply not included in your question, but the best way to check for an empty array is to basically look where the 0 index does not match $exists:
Inventory.update(
{ "products.rates.0": { "$exists": false } },
{
"$pull": {
"products": { "rates.0": { "$exists": false } }
}
},
{ "multi": true },
function(err,numAffected) {
}
)
The "query" portion of the .update() statement is making sure that we only even attempt to touch documents which have an empty array in "products.rates". That isn't required, but it does avoid testing the following "update" statement condition on documents where that condition is not true for any array element, and thus makes things a bit faster.
The actual "update" portion applies $pull on the "products" array to remove any of those items where the "inner" "rates" is an empty array. So the "path" within the $pull is actually looking inside the "products" content anyway, so it is relative to that and not to the whole document.
Naturally $pull will remove all elements that match in a single operation. The "multi" is only needed when you really want to update more than one document with the statement
I'm building a chat app, that should retrieve all new messages from MongoDB, grouped in to conversations. But each message should have a new 'is_self' field
Edit:
The 'is_self' field contains a boolean for if the message if from the user.
so pseudo:
is_self: {$cond: {if: {message.sender == MYID)}, then: true, else: false}
So lets say I have Message model
var MessageSchema = new Schema({
conversation_id:{type: mongoose.Schema.ObjectId, ref: 'Conversation', required: true},
message: {type: String, required: true},
sender: {type: mongoose.Schema.ObjectId, ref: 'User'},
created: {type: Date, default: Date.now},
read: {type: Boolean, default: false}
});
And a Conversation model
var ConversationSchema = new Schema({
from: {type: mongoose.Schema.ObjectId, ref: 'User', required: true},
to: {type: mongoose.Schema.ObjectId, ref: 'User', required: true},
last_changed: {type: Date, default: Date.now},
created: {type: Date, default: Date.now}
});
Now I try to do an Aggregate, to load all messages that are inside a conversation_id array and are created > last_checked date...
So it looks like this:
mongoose.model("Message").aggregate([
// First find all messages
{
$match: {
$and: [{conversation_id: {$in: idArray}}, {created: {$gt: lastChecked}}]
}
},
// Add is self field
{
$group: {
_id: $_id,
$is_self: {
$cond: {'if(message.sender == MYID then true else false': '??'}
}
}
},
// Sort by date
{$sort: {created: -1}},
// Then group by conversation
{
$group: {
_id: '$conversation_id',
messages: {
$push: '$$ROOT'
},
}
}
// TODO: find users for unknown conversation
/*,
{
$project: {
user: {
$or: [{conversation_id: {$in: knownConversations}}]
}
}
}*/
])
I tried with $cond and if / else statement but Mongo doesn't allow that..
Thanks!
Simple usage of the $eq operator which returns boolean. Also $push will take any object format you throw at it:
var senderId = // whatever;
mongooose.model("Message").aggregate([
{ "$match": {
"conversation_id": { "$in": idArray },
"created": { "$gt": lastChecked }
}},
{ "$group": {
"_id": "$conversation_id",
"messages": {
"$push": {
"message": "$message",
"is_self": {
"$eq": [ "$sender", senderId ]
}
}
}
}}
// whatever else
],function(err,results) {
})
If you want, then combine with $cond to alternately add "is_self" only when detected:
{ "$group": {
"_id": "$conversation_id",
"messages": {
"$push": {
"$cond": [
{ "$eq": [ "$sender", senderId] },
{
"message": "$message",
"is_self": true
},
{
"messsage": "$message"
}
]
}
}
}}