From the query below
let fields = { 'local.email': 1 };
UserModel.find({ '_id': { $in: userIds } }).select(fields).setOptions({ lean: true });
Result which we get is
[
{
"_id": "54bf2d7415eaaa570c9ed5a0",
"local": {
"email": "neo#q.com"
}
},
{
"_id": "54bfb753e4c9406112267056",
"local": {
"email": "test#q.com"
}
}
]
Is is possible to modify query itself to get below result
["neo#q.com", "test#q.com"]
Thanks in advance
You could use aggregate to return a list of objects with the emails and the map them to an array of strings:
const emailObjs = await UserModel.aggregate([
{
$match: {
_id: {
$in: userIds
}
}
},
{
$project: {
"_id": 0,
"email": "$local.email"
}
}
]);
const emails = emailObjs.map(obj => obj.email)
Link to playground for the query.
im building a backend for a dummy ecommerce app for my portfolio in nodejs using express and mongoose, in this moment a have an Order model that give me this kind of response:
"order": {
"_id": "62c89ae8aca0f50e4ced43ea",
"shippingFee": 15000,
"subtotal": 625000,
"total": 640000,
"orderItems": [
{
"name": "Air Zoom Alphafly",
"image": "/uploads/calzado-de-carrera-de-carretera-air-zoom-alphafly-next-flyknit-mGK8M0.jpeg",
"color": "purple",
"price": 625000,
"amount": 1,
"product": "62be1d1a70c464d05c92485f",
"_id": "62c89ae8aca0f50e4ced43eb"
}
],
"status": "canceled",
"user": "62c72bf9d304f89a8b65fe3c",
"methodPayment": "transferencia",
"createdAt": "2022-07-08T21:00:24.611Z",
"updatedAt": "2022-07-13T00:05:27.124Z",
"__v": 0
}
What Im trying to do is build a query that search for an specific product in the orderItems array and create a new item that add the price for that product. In this moment in my backend I have this code:
export const getMonthlyIncome = async (req, res) => {
const productId = req.query.pid;
const date = new Date();
const lastMonth = new Date(date.setMonth(date.getMonth() - 1));
const previousMonth = new Date(new Date().setMonth(lastMonth.getMonth() - 1));
try {
const income = await Order.aggregate([
{
$match: {
createdAt: { $gte: previousMonth },
status: "paid",
...(productId && {
orderItems: { $elemMatch: { product: productId } },
}),
},
},
{
$project: {
month: { $month: "$createdAt" },
sales: "$total",
productSales: "orderItems.price",
},
},
{
$group: {
_id: "$month",
total: { $sum: "$sales" },
test: { $sum: "$productSales" },
},
},
]);
res.status(StatusCodes.OK).json({ income });
} catch (error) {
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json(error);
}
};
When I test the endpoint in postman the result is an empty array.
What im doing wrong? I hope somebody can help me, thanks in advance.
I am building an API to store friends names for a game, I have built the API to receive the post request as so :
exports.addFriends = async (req, res) => {
try {
console.log('hit');
console.log(req.body.friendNames);
const addUser = await User.updateOne(
{ uniqueid: req.body.uniqueid },
{ $push: { friendNames: [req.body.friendNames] } }
);
res.json({
addUser
});
} catch (error) {
console.log(error);
}
};
ad the post request as
const friends = await axios.post('/api/v1/users/add/friends', {
uniqueId: this.uniqueid,
friendNames: [
{
userName: 'test',
region: 'euw'
}
]
});
My API is being hit as a see the logs, but no record is made. My User Schema is as so
const userSchema = new mongoose.Schema({
uniqueid: {
type: String,
required: true,
trim: true
},
summonerName: {
type: String
},
friendNames: [
{
userName: String,
region: String
}
]
});
I get no error and the request seems to go through, but no records are added. Any ideas?
$push is used to add one element to the array. But using the $each array update operator, we can push an array of items.
Also, I used findOneAndUpdate with new:true option to retrieve the updated document, because updateOne doesn't return the updated document.
exports.addFriends = async (req, res) => {
try {
console.log(req.body.friendNames);
const addUser = await User.findOneAndUpdate(
{ uniqueid: req.body.uniqueid },
{ $push: { friendNames: { $each: req.body.friendNames } } },
{ new: true }
);
res.json({ addUser });
} catch (error) {
console.log(error);
res.status(500).send("Something went wrong");
}
}
Let's say we have this existing document:
{
"_id": "5e31c749f26d5f242c69f3aa",
"uniqueid": "uniqueid1",
"summonerName": "John",
"friendNames": [
{
"_id": "5e31c749f26d5f242c69f3ab",
"userName": "Max",
"region": "Germany"
}
],
"__v": 0
}
Let's send a request to the controller with this request body:
{
"uniqueid": "uniqueid1",
"friendNames": [
{
"userName": "Andrew",
"region": "England"
},
{
"userName": "Smith",
"region": "USA"
}
]
}
The response will be like this:
{
"addUser": {
"_id": "5e31c749f26d5f242c69f3aa",
"uniqueid": "uniqueid1",
"summonerName": "John",
"friendNames": [
{
"_id": "5e31c749f26d5f242c69f3ab",
"userName": "Max",
"region": "Germany"
},
{
"_id": "5e31c763f26d5f242c69f3ad",
"userName": "Andrew",
"region": "England"
},
{
"_id": "5e31c763f26d5f242c69f3ac",
"userName": "Smith",
"region": "USA"
}
],
"__v": 0
}
}
This is the first model. It's located in the folder called models/user.js
'use strict'
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = Schema({
publications: [{
description: String,
categories: [{
type: Schema.Types.ObjectId,
ref: 'Category.subcategories'
}]
}]
The model category. It's located in the folder called models/category.js
'use strict'
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var CategorySchema = Schema({
name: String,
subcategories: [{
name: String
}]
});
Look that The UserSchema has categories. This one has this reference:
ref: 'Category.subcategories'
I have a folder called controller. The next json is from controller/category.js
'use strict'
var Categories = require('./models/category');
function getCategories(req, res){
Categories.find({},(err, categories) =>{
if(err){
res.status(500).send({ message: 'Error en la peticion' });
return;
}
if(!categories){
res.status(404).send({ message: 'No hay categories' });
return
}
res.status(200).send({ categories });
});
}
module.exports = {
getCategories
}
The json of Category looks like this.
{
"categories": [
{
"subcategories": [
{
"_id": "5ae4a8b0a7510e3bd80917db",
"name": "subcategory1"
},
{
"_id": "5ae4a8b0a7510e3bd80917da",
"name": "subcategory2"
},
{
"_id": "5ae4a8b0a7510e3bd80917d9",
"name": "subcategory3"
}
],
"_id": "5ae4a8b0a7510e3bd80917d7",
"name": "Category1",
"__v": 0
},
{
"subcategories": [
{
"_id": "5ae4a8b0a7510e3bd80917e0",
"name": "subcategory1"
},
{
"_id": "5ae4a8b0a7510e3bd80917df",
"name": "subcategory2"
}
],
"_id": "5ae4a8b0a7510e3bd80917dc",
"name": "Category2",
"__v": 0
}
]
}
Each subcategory2 belongs to one Category.
This one is in controller/user.js
The user whithout populate will looks like this
User.find({}, (err, publications) => {
if (err) {
res.status(500).send({
message: "Error en la peticion " + err
});
return;
}
if (!publications) {
res.status(404).send({
message: "Publicacion no encontrada"
});
return;
}
res.status(200).send(publications);
result
"publications": [
{
"description": "abc",
"categories": [
"5ae4a8b0a7510e3bd80917db",
"5ae4a8b0a7510e3bd80917da"
]
},
{
"description": "abcddvas asa",
"categories": [
"5ae4a8b0a7510e3bd80917e0"
]
},
]
I need to populate categories when I do this.
User.find().populate('publications.categories').then(function (err, posa) {
if (err) {
res.status(500).send(err);
return;
}
res.status(500).send(posa);
return;
});
So the final json should look like this:
"publications": [
{
"description": "abc",
"categories": [
{
"_id": "5ae4a8b0a7510e3bd80917db",
"name": "subcategory1"
},
{
"_id": "5ae4a8b0a7510e3bd80917da",
"name": "subcategory2"
}
]
},
{
"description": "abcddvas asa",
"categories": [
{
"_id": "5ae4a8b0a7510e3bd80917e0",
"name": "subcategory1"
}
]
},
]
But The result does not show me the final json.
I see that there is a option called Dynamic References.
http://mongoosejs.com/docs/populate.html
The example show this
var userSchema = new Schema({
name: String,
connections: [{
kind: String,
item: { type: ObjectId, refPath: 'connections.kind' }
}]
});
Instead of use ref, uses refPath. But for me didn't work that.
I tried to use this
https://github.com/buunguyen/mongoose-deep-populate
but that didn't work for me.
I tried to do a lot of tries. None works for me. I have been searching days and trying a lot of things but I have not been able to resolve this.
I use Nodejs, Hapijs and Mongoose.
I 've a schema and model as follows.
var schema = {
name: {
type: String,
required: true
},
lectures: {}
};
var mongooseSchema = new mongoose.Schema(schema, {
collection: "Users"
});
mongoose.model("Users", mongooseSchema);
For some reason, I need to keep "lectures"
as mixed type.
While saving/creating a document I create a nested property lectures.physics.topic[] where topic is an array.
Now, I'm trying to add/push a new object to "lectures.physics.topic" using $addToSet or $push.
userModel.findByIdAndUpdateAsync(user._id, {
$addToSet: {
"lectures.physics.topic": {
"name": "Fluid Mechanics",
"day": "Monday",
"faculty": "Nancy Wagner"
}
}
});
But the document is simply not getting updated. I tried using $push too. Nothing worked. What could be the problem?
I tried to another approach using mongoclient , to update the db directly .It works please find the below code which works
db.collection("Users").update({
"_id": user._id
}, {
$addToSet: {
"lectures.physics.topic": {
"name": "Fluid Mechanics",
"day": "Monday",
"faculty": "Nancy Wagner"
}
}
}, function(err, result) {
if (err) {
console.log("Superman!");
console.log(err);
return;
}
console.log(result);
});
I have to start the mongo client every time a request is hit.This is not a feasible solution.
Mongoose loses the ability to auto detect and save changes made on Mixed types so you need to "tell" it that the value of a Mixed type has changed by calling the .markModified(path) method of the document passing the path to the Mixed type you just changed:
doc.mixed.type = 'changed';
doc.markModified('mixed.type');
doc.save() // changes to mixed.type are now persisted
In your case, you could use findById() method to make your changes by calling the addToSet() method on the topic array and then triggering the save() method to persist the changes:
userModel.findById(user._id, function (err, doc){
var item = {
"name": "Fluid Mechanics",
"day": "Monday",
"faculty": "Nancy Wagner"
};
doc.lectures.physics.topic.addToSet(item);
doc.markModified('lectures');
doc.save() // changes to lectures are now persisted
});
I'd be calling "bug" on this. Mongoose is clearly doing the wrong thing as can be evidenced in the logging as shown later. But here is a listing that calls .findOneAndUpdate() from the native driver with the same update you are trying to do:
var async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/school');
mongoose.set('debug',true);
var userSchema = new Schema({
name: {
type: String,
required: true
},
lectures: { type: Schema.Types.Mixed }
});
var User = mongoose.model( "User", userSchema );
function logger(data) {
return JSON.stringify(data, undefined, 2);
}
async.waterfall(
[
function(callback) {
User.remove({},function(err) {
callback(err);
});
},
function(callback) {
console.log("here");
var user = new User({ "name": "bob" });
user.save(function(err,user) {
callback(err,user);
});
},
function(user,callback) {
console.log("Saved: %s", logger(user));
User.collection.findOneAndUpdate(
{ "_id": user._id },
{
"$addToSet": {
"lectures.physics.topic": {
"name": "Fluid Mechanics",
"day": "Monday",
"faculty": "Nancy Wagner"
}
}
},
{ "returnOriginal": false },
function(err,user) {
callback(err,user);
}
);
}
],
function(err,user) {
if (err) throw err;
console.log("Modified: %s", logger(user));
mongoose.disconnect();
}
);
This works perfectly with the result:
Saved: {
"__v": 0,
"name": "bob",
"_id": "55cda1f5b5ee8b870e2f53bd"
}
Modified: {
"lastErrorObject": {
"updatedExisting": true,
"n": 1
},
"value": {
"_id": "55cda1f5b5ee8b870e2f53bd",
"name": "bob",
"__v": 0,
"lectures": {
"physics": {
"topic": [
{
"name": "Fluid Mechanics",
"day": "Monday",
"faculty": "Nancy Wagner"
}
]
}
}
},
"ok": 1
}
You neeed to be careful here as native driver methods are not aware of the connection status like the mongoose methods are. So you need to be sure a connection has been made by a "mongoose" method firing earlier, or wrap your app in a connection event like so:
mongoose.connection.on("connect",function(err) {
// start app in here
});
As for the "bug", look at the logging output from this listing:
var async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/school');
mongoose.set('debug',true);
var userSchema = new Schema({
name: {
type: String,
required: true
},
lectures: { type: Schema.Types.Mixed }
});
var User = mongoose.model( "User", userSchema );
function logger(data) {
return JSON.stringify(data, undefined, 2);
}
async.waterfall(
[
function(callback) {
User.remove({},function(err) {
callback(err);
});
},
function(callback) {
console.log("here");
var user = new User({ "name": "bob" });
user.save(function(err,user) {
callback(err,user);
});
},
function(user,callback) {
console.log("Saved: %s", logger(user));
User.findByIdAndUpdate(
user._id,
{
"$addToSet": {
"lectures.physics.topic": {
"name": "Fluid Mechanics",
"day": "Monday",
"faculty": "Nancy Wagner"
}
}
},
{ "new": true },
function(err,user) {
callback(err,user);
}
);
}
],
function(err,user) {
if (err) throw err;
console.log("Modified: %s", logger(user));
mongoose.disconnect();
}
);
And the logged output with mongoose logging:
Mongoose: users.remove({}) {}
here
Mongoose: users.insert({ name: 'bob', _id: ObjectId("55cda2d2462283c90ea3f1ad"), __v: 0 })
Saved: {
"__v": 0,
"name": "bob",
"_id": "55cda2d2462283c90ea3f1ad"
}
Mongoose: users.findOne({ _id: ObjectId("55cda2d2462283c90ea3f1ad") }) { new: true, fields: undefined }
Modified: {
"_id": "55cda2d2462283c90ea3f1ad",
"name": "bob",
"__v": 0
}
So in true "What the Fudge?" style, there is a call there to .findOne()? Which is not what was asked. Moreover, nothing is altered in the database of course because the wrong call is made. So even the { "new": true } here is redundant.
This happens at all levels with "Mixed" schema types.
Personally I would not nest within "Objects" like this, and just make your "Object keys" part of the standard array as additional properties. Both MongoDB and mongoose are much happier with this, and it is much easier to query for information with such a structure.
var async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/school');
mongoose.set('debug',true);
var lectureSchema = new Schema({
"subject": String,
"topic": String,
"day": String,
"faculty": String
});
var userSchema = new Schema({
name: {
type: String,
required: true
},
lectures: [lectureSchema]
});
var User = mongoose.model( "User", userSchema );
function logger(data) {
return JSON.stringify(data, undefined, 2);
}
async.waterfall(
[
function(callback) {
User.remove({},function(err) {
callback(err);
});
},
function(callback) {
console.log("here");
var user = new User({ "name": "bob" });
user.save(function(err,user) {
callback(err,user);
});
},
function(user,callback) {
console.log("Saved: %s", logger(user));
User.findByIdAndUpdate(
user._id,
{
"$addToSet": {
"lectures": {
"subject": "physics",
"topic": "Fluid Mechanics",
"day": "Monday",
"faculty": "Nancy Wagner"
}
}
},
{ "new": true },
function(err,user) {
callback(err,user);
}
);
}
],
function(err,user) {
if (err) throw err;
console.log("Modified: %s", logger(user));
mongoose.disconnect();
}
);
Output:
Mongoose: users.remove({}) {}
here
Mongoose: users.insert({ name: 'bob', _id: ObjectId("55cda4dc40f2a8fb0e5cdf8b"), lectures: [], __v: 0 })
Saved: {
"__v": 0,
"name": "bob",
"_id": "55cda4dc40f2a8fb0e5cdf8b",
"lectures": []
}
Mongoose: users.findAndModify({ _id: ObjectId("55cda4dc40f2a8fb0e5cdf8b") }) [] { '$addToSet': { lectures: { faculty: 'Nancy Wagner', day: 'Monday', topic: 'Fluid Mechanics', subject: 'physics', _id: ObjectId("55cda4dc40f2a8fb0e5cdf8c") } } } { new: true, upsert: false, remove: false }
Modified: {
"_id": "55cda4dc40f2a8fb0e5cdf8b",
"name": "bob",
"__v": 0,
"lectures": [
{
"faculty": "Nancy Wagner",
"day": "Monday",
"topic": "Fluid Mechanics",
"subject": "physics",
"_id": "55cda4dc40f2a8fb0e5cdf8c"
}
]
}
So that works fine, and you don't need to dig to the native methods just to make it work.
Properties of an array make this much easy to query and filter, as well as "aggregate" information across the data, which for all of those MongoDB likes a "strict path" to reference all information. Otherwise you are diffing to only "specific keys", and those cannot be indexed or really searched without mentioning every possible "key combination".
Properties like this are a better way to go. And no bugs here.