I have a find() query that when executed, I can see the json with the nested schemas that I want to see except for the 'artista' attribute only displays the id, instead of the properties I want. See below:
{
"total": 1,
"ordenes": [
{
"artpieces": [
{
"_id": "60c1388f30316c02b9f6351f",
"artista": "60c055736c7ca511055a0e1a",
"nombre": "LILIES"
},
{
"_id": "60c12fca30316c02b9f63519",
"nombre": "GERNICA",
"artista": "60c136bf30316c02b9f6351b"
}
],
"_id": "60c414f9ea108a14ef75a9fb",
"precio": 3000,
"usuario": {
"_id": "609c0068e67e68",
"nombre": "Arturo Filio"
}
}
]
}
The query I use to get the json above:
const [total, ordenes] = await Promise.all([
Orden.countDocuments(),
Orden.find()
.populate("usuario", "nombre")
.populate("artpieces", ["nombre","artista","nombre"])
]);
res.json({
total,
ordenes
});
It's an order schema that has artpieces. Each artpiece (I called it 'producto'), has a name a genre, an author/artist and the user which the order belongs to.
my Schema for the orden.js:
const { Schema, model } = require('mongoose');
const OrdenSchema = Schema({
artpieces: [
{
type: Schema.Types.ObjectId,
required: true,
ref: 'Producto'
}
],
estado: {
type: Boolean,
default: true,
required: true
},
usuario: {
type: Schema.Types.ObjectId,
ref: 'Usuario',
required: true
},
precio: {
type: Number,
required: true
}
})
OrdenSchema.methods.toJSON = function () {
const { __v, estado, ...data} = this.toObject();
return data;
}
module.exports = model('Orden', OrdenSchema);
Last thing I want to mention, I know for a fact that I have the code necessary in the artista.js model to display the name of the artist because I have a similar query to display all the artpieces with each artpiece have a genre and an artist.
That example looks like so (to give context):
{
"total": 4,
"productos": [
{
"precio": 0,
"_id": "60c12fca30316c02b9f63519",
"nombre": "GERNICA",
"categoria": {
"_id": "60c04e3605d3c10ed10389e4",
"nombre": "NEO CUBISMO"
},
"artista": {
"_id": "60c136bf30316c02b9f6351b",
"nombre": "PICASSO"
},
"usuario": {
"_id": "609c8c0068e67e68",
"nombre": "Arturo Filio"
}
}
]
}
What am I doing wrong that I can't get my json result at the top look like the json at the bottom, where the artist attribute is?
Also just to point out, I have checked how to nest populate methods in order SO posts including the path and the ref to the Schema and still haven't been able to get the expected result.
I am creating a schema for an Order model that will track the items ordered along with the quantity purchased. I want to keep the itemId references and the quantity tied together as an array in one parameter.
I have created an Array that includes a reference to the ObjectId plus an additional Number type. I am currently unable to populate the product information using a .populate() query.
Order Schema
const mongoose = require("mongoose");
const { Schema } = mongoose;
const orderSchema = new Schema({
orderNumber: String,
_itemsOrdered: [
{
itemId: {
type: mongoose.Schema.Types.ObjectId,
ref: "menuItems"
},
quantity: Number
}
]
});
mongoose.model("orders", orderSchema);
MenuItem Schema
const mongoose = require("mongoose");
const { Schema } = mongoose;
const MenuItemSchema = new Schema({
imageURL: String,
name_en: String,
name_es: String,
type_en: String,
type_es: String,
description_en: String,
description_es: String,
dietaryCallouts: [String],
price: Number
});
mongoose.model("menuItems", MenuItemSchema);
module.export = MenuItemSchema;
I am able to save the record but cannot populate the MenuItem information with the following query:
Order Controller
async show(req, res, next) {
try {
const orderId = req.params.id;
let order = await Order.findById({ _id: orderId }).populate(
"_itemsOrdered.itemId"
);
res.send(order);
} catch (err) {
res.status(402).send(err);
}
}
Here it the order object that is being saved to the DB.
Order Object
{
"_id": "5dc93b9c0085b8045e0c8aa3",
"orderNumber": "Order 3",
"_itemsOrdered": [
{
"_id": "5dc93b9c0085b8045e0c8aa5",
"itemId": "5dc7f814a2679b47319a79a4",
"quantity": 1
},
{
"_id": "5dc93b9c0085b8045e0c8aa4",
"itemId": "5dc7e5c7de590744c46f93da",
"quantity": 2
}
],
"__v": 0
}
Your order schema must be like this:
const orderSchema = new Schema({
orderNumber: String,
_itemsOrdered: [
{
itemId: { type: mongoose.Schema.Types.ObjectId, ref: "menuItems" },
quantity: Number
}
]
});
And you can use the following route to create an order document.
router.post("/order", async (req, res, next) => {
try {
const { orderNumber, _itemsOrdered } = req.body;
let order = new Order({ orderNumber, _itemsOrdered });
order = await order.save();
res.status(201).send(order);
} catch (err) {
console.log(err);
res.status(500).send(err);
}
});
Sample body: (you need to change ids according to yours)
{
"orderNumber": "Order 1",
"_itemsOrdered": [
{"itemId": "5dc90346222b892434e4675a", "quantity" : 1 },
{"itemId": "5dc90359222b892434e4675b", "quantity" : 2 }
]
}
To get the order and its items you can use populate like this:
router.get("/orders/:id", async (req, res) => {
try {
const orderAndItems = await Order.findById(req.params.id).populate(
"_itemsOrdered.itemId"
);
res.send(orderAndItems);
} catch (err) {
console.log(err);
res.status(500).send(err);
}
});
This will give you a result like this:
{
"_id": "5dc904db8407a217b4dfe6f4",
"orderNumber": "Order 1",
"_itemsOrdered": [
{
"_id": "5dc904db8407a217b4dfe6f6",
"itemId": {
"_id": "5dc90346222b892434e4675a",
"name_en": "item 1",
"price": 1,
"__v": 0
},
"quantity": 1
},
{
"_id": "5dc904db8407a217b4dfe6f5",
"itemId": {
"_id": "5dc90359222b892434e4675b",
"name_en": "item 2",
"price": 2,
"__v": 0
},
"quantity": 2
}
],
"__v": 0
}
This is my controller file locations.js
var mongoose = require('mongoose');
var Loc = mongoose.model('location');
module.exports.locationsListByDistance = function(req, res) {
var lng = parseFloat(req.query.lng);
var lat = parseFloat(req.query.lat);
var point = {
type: "Point",
coordinates: [lng, lat]
};
var geoOptions = {
spherical: true,
maxDistance: 1000
};
Loc.geoNear(point, geoOptions, function (err, results, stats) {
console.log(results);
});
};
My model file locations.js
var mongoose = require('mongoose');
var reviewSchema = new mongoose.Schema({
author: String,
rating: {
type: Number,
required: true,
min: 0,
max: 5
},
reviewText: String,
createdOn: {
type: Date,
"default": Date.now
}
});
var openingTimeSchema = new mongoose.Schema({
days: {
type: String,
required: true
},
opening: String,
closing: String,
closed: {
type: Boolean,
required: true
}
});
var locationSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
address: String,
rating: {
type: Number,
"default": 0,
min: 0,
max: 5
},
facilities: [String],
// Always store coordinates longitude, latitude order.
coords: {
type: [Number],
index: '2dsphere'
},
openingTimes: [openingTimeSchema],
reviews: [reviewSchema]
});
mongoose.model('location', locationSchema, 'locations');
Whenever I run http://localhost:3000/api/locations?lng=-0.9690884&lat=51.455041 I get error geoNear is not a function
TypeError: Loc.geoNear is not a function
at module.exports.locationsListByDistance (/home/shackers/Projects/mean/loc8r/app_api/controllers/locations.js:51:7)
at Layer.handle [as handle_request] (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/layer.js:95:5)
at next (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/layer.js:95:5)
at /home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:335:12)
at next (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:174:3)
at router (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:317:13)
at /home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:335:12)
at next (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:275:10)
at /home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:635:15
This are versions of dependencies i am using:
node : 8.9.3 npm : 5.5.1 express : 4.15.5 mongoose : 5.0.0
mongoDb : 3.6.1
router.get('/', () => {
Loc.aggregate([
{
$geoNear: {
near: 'Point',
distanceField: "dist.calculated",
maxDistance: 100000,
spherical: true
}
}
]).then(function(err, results, next){
res.send();
}).catch(next);
});
Ref:- https://docs.mongodb.com/manual/reference/command/geoNear/
This error is happening because .geoNear used to be supported, but is no longer supported as of Mongoose 5, which uses the Node MongoDB v3 driver.
The issue is documented in the Migrating to Mongoose 5 document, which in turn links to the MongoDB 3 drive release notes which provides this statement about recommend replacements:
The functionality of the geoNear command is duplicated elsewhere in the language, in the $near/$nearSphere query operators on unsharded collections, and in the $geoNear aggregation stage on all collections.
Effectively, the official docs are endorsing kind of use of $geoNear documented in other answers.
I'm having the exact same problem and I've abandoned the approach I was using before (which looks like you were having too). The following is an alternative that does not throw an error and should give you the same result you were after using Loc.geoNear:
Loc.aggregate(
[
{
'$geoNear': {
'near': point,
'spherical': true,
'distanceField': 'dist',
'maxDistance': 1000
}
}
],
function(err, results) {
// do what you want with the results here
}
)
Apparently I'm in the same book (Getting Mean, Manning) and running into roughly the same issues. This seems to work for me:
var mongoose = require('mongoose');
var Loc = mongoose.model('Location');
var sendJSONresponse = function(res, status, content) {
res.status(status);
res.json(content);
};
var theEarth = (function() {
console.log('theEarth');
var earthRadius = 6371; // km, miles is 3959
var getDistanceFromRads = function(rads) {
return parseFloat(rads * earthRadius);
};
var getRadsFromDistance = function(distance) {
return parseFloat(distance / earthRadius);
};
return {
getDistanceFromRads: getDistanceFromRads,
getRadsFromDistance: getRadsFromDistance
};
})();
/* GET list of locations */
module.exports.locationsListByDistance = function(req, res) {
console.log('locationsListByDistance:');
var lng = parseFloat(req.query.lng);
var lat = parseFloat(req.query.lat);
var maxDistance = parseFloat(req.query.maxDistance);
var point = {
type: "Point",
coordinates: [lng, lat]
};
console.log('point: ' + point)
var geoOptions = {
spherical: true,
maxDistance: theEarth.getRadsFromDistance(maxDistance),
num: 10
};
console.log('geoOptions: ' + geoOptions);
if ((!lng && lng!==0) || (!lat && lat!==0) || ! maxDistance) {
console.log('locationsListByDistance missing params');
sendJSONresponse(res, 404, {
"message": "lng, lat and maxDistance query parameters are all required"
});
return;
} else {
console.log('locationsListByDistance running...');
Loc.aggregate(
[{
'$geoNear': {
'near': point,
'spherical': true,
'distanceField': 'dist.calculated',
'maxDistance': maxDistance
}
}],
function(err, results) {
if (err) {
sendJSONresponse(res, 404, err);
} else {
locations = buildLocationList(req, res, results);
sendJSONresponse(res, 200, locations);
}
}
)
};
};
var buildLocationList = function(req, res, results) {
console.log('buildLocationList:');
var locations = [];
results.forEach(function(doc) {
locations.push({
distance: doc.dist.calculated,
name: doc.name,
address: doc.address,
rating: doc.rating,
facilities: doc.facilities,
_id: doc._id
});
});
return locations;
};
returns a result list similar to such:
[
{
"distance": 0,
"name": "Rathaus",
"address": "Markt",
"rating": 0,
"facilities": [
"museum"
],
"_id": "5a9366517775811a449e503e"
},
{
"distance": 61.77676881925853,
"name": "Haus Löwenstein",
"address": "",
"rating": 0,
"facilities": [
"museum"
],
"_id": "5a9366517775811a449e5045"
},
{
"distance": 63.03445976427102,
"name": "Goldener Schwan",
"address": "Markt 37",
"rating": 0,
"facilities": [
"restaurant"
],
"_id": "5a9366517775811a449e5052"
},
{
"distance": 66.60375653163021,
"name": "Klein Printenbäckerei",
"address": "Krämerstraße 12",
"rating": 0,
"facilities": [
"supermarket"
],
"_id": "5a9366517775811a449e504d"
},
{
"distance": 74.91278395082011,
"name": "Couven-Museum",
"address": "Hühnermarkt 17",
"rating": 0,
"facilities": [
"museum"
],
"_id": "5a9366517775811a449e5042"
},
{
"distance": 132.2939512054143,
"name": "Cathedral Treasury",
"address": "Johannes-Paul-II.-Straße",
"rating": 0,
"facilities": [
"museum"
],
"_id": "5a9366517775811a449e503d"
},
{
"distance": 152.11867357742042,
"name": "Aachen Cathedral",
"address": "Domhof 1",
"rating": 0,
"facilities": [
"museum"
],
"_id": "5a9366517775811a449e503c"
},
{
"distance": 155.92015153163268,
"name": "International Newspaper Museum",
"address": "Pontstraße 13",
"rating": 0,
"facilities": [
"museum"
],
"_id": "5a9366517775811a449e5040"
},
{
"distance": 175.0857109968383,
"name": "Nobis Printen",
"address": "Münsterplatz 3",
"rating": 0,
"facilities": [
"supermarket"
],
"_id": "5a9366517775811a449e504c"
},
{
"distance": 179.32348875834543,
"name": "Grashaus",
"address": "Fischmarkt",
"rating": 0,
"facilities": [
"museum"
],
"_id": "5a9366517775811a449e5044"
},
{
"distance": 189.8675948747873,
"name": "Maranello",
"address": "Pontstraße 23",
"rating": 0,
"facilities": [
"restaurant"
],
"_id": "5a9366517775811a449e5057"
},
{
"distance": 198.2239741555585,
"name": "Carlos I",
"address": "Rennbahn 1",
"rating": 0,
"facilities": [
"restaurant"
],
"_id": "5a9366517775811a449e5055"
}
]
Not sure how accurate it is - got a list of addresses loaded and not 100% sure what's close to what in the random mess... but it returns a list and I'll test correctness somehow at some point.
I found the solution. Just downgrade mongoose and install version 4.9.1. Latest release of mongoose does not support Loc.geoNear
npm remove mongoose
npm install mongoose#4.9.1
More straightforward IMO, than the previous two answers in the Grider's course is:
index(req, res, next) {
const { lng, lat } = req.query;
Driver.find({
'geometry.coordinates': {
$nearSphere: {
$geometry: {
type: 'Point',
coordinates:[parseFloat(lng), parseFloat(lat)]
},
$maxDistance: 200000,
},
}
})
.then(drivers => res.send(drivers))
.catch(next);
}
This is in the spirit of the original definition he gives and uses the new functions which do the same thing as the old geoNear, except they've split out the spherical and non-spherical versions now. You'll need:
beforeEach((done) => {
const { drivers } = mongoose.connection.collections;
drivers.drop()
.then(() => drivers.createIndex({ 'geometry.coordinates': '2dsphere' }))
.then(() => done())
.catch(() => done());
};
In the test helper as mentioned before.
I think you're looking for this, please correct me if there is mistake there.
module.exports.locationsListBydistance = function (req, res) {
var lng = parseFloat(req.query.lng);
var lat = parseFloat(req.query.lat);
Loc.aggregate(
[{
$geoNear: {
'near': {'type':'Point', 'coordinates':[lng, lat]},
'spherical': true,
'maxdistance': theEarth.getRadsFromDistance(20),
'num':10,
'distanceField': 'dist'
}
}
], function(err, results) {
var locations = [];
console.log(results);
results.forEach(function (doc) {
locations.push({
distance: theEarth.getDistanceFromRads(doc.dist),
name: doc.name,
address: doc.address,
facilities: doc.facilities,
rating: doc.rating,
_id: doc._id
});
});
sendJsonResponse(res, 200, locations);
});
};
router.get('/',function(req,res,next){
Loc.aggregate([
{
$geoNear: {
near: {type:'Point', coordinates:[parseFloat(req.query.lng), parseFloat(req.query.lat)]},
distanceField: "dist.calculated",
maxDistance: 1000,
spherical: true
}
}
]).then(function(Locs){
res.send(Locs)
})
})
Model.geoNear() has been removed because the MongoDB driver no longer supports it
Model.geoNear() has been removed because the MongoDB driver no longer supports it, so you should use the aggregation method for example :
for mongoose v4.2:
my code was like this :
Modal.geoNear(
{type: 'Point', coordinates: [parseFloat(req.query.lng), parseFloat(req.query.lat)]},
{maxDistance: 100000, spherical: true}
)
.then(results => {
res.send(results);
})
for the latest mongoose version :
the code is :
Modal.aggregate([{
$geoNear: {
near: {
type: 'Point',
coordinates: [parseFloat(req.query.lng), parseFloat(req.query.lat)]
},
distanceField: 'distance',
maxDistance: 100000,
spherical: true
}
}])
.then(results => {
res.send(results);
})
Hope i answer or solve your problem, happy coding :D
The answer given by user : phao5814 is quite right I tried it out and must say It worked out well for me
index(req, res, next)
{
const { lng, lat } = req.query;
Driver.aggregate([
{
'$geoNear': {
"near": { 'type': 'Point',
'coordinates': [parseFloat(lng), parseFloat(lat)] },
"spherical": true,
"distanceField": 'dist',
"maxDistance": 200000
}
}
])
.then(drivers => res.send(drivers))
.catch(next);
}
I used Mongo 3.2 on debian 8. Node 5.11.1 and Mongoose.
The object are stored
{
"_id": {
"$oid": "5756e710eb8863716246689a"
},
"id": 735842,
"employee_id": 477,
"matricule": "1020510",
"name": "",
"date_day": "2016-04-24T22:00:00.000Z",
"morning_status": "P",
"morning_time_start": "08:16",
"morning_time_end": "12:12",
"afternoon_status": "P",
"afternoon_time_start": "12:57",
"afternoon_time_end": "18:30",
"working_time_theorical": 28800,
"working_time_employee": 34140,
"extra_hours": 5340,
"extra_hours_week": 10680,
"extra_hours_month": 78120,
"comments": ""
}
I want to extract the data with the field : date_day
var reporticoSchema = mongoose.Schema({
id :
{
type:Number,
index:true,
required:true
},
employee_id :
{
type:Number,
index:true,
required:true
},
matricule :
{
type:String,
index:true,
required:true
},
date_day :
{
type: Date,
index:true
},
.....
var Reportico=module.exports = mongoose.model('Reportico',reporticoSchema)
var dateStart=new Date('2016-04-01').toISOString(),
dateEnd=new Date('2016-04-30').toISOString();
console.log('d',dateStart,dateEnd)
var employeeId={employee_id:521,date_day:{$gte:dateStart,$lte:dateEnd}};
//console.log('employeeId',employeeId)
Reportico.find(employeeId).exec(function(err, employees) {
console.log('result')
if (err)return console.log('err',err);
console.log('employees',employees)
})
I updated the code to add te format of the field date_day
The result is empty.
This is my consult with date, I using library http://momentjs.com/ to separate date
var searchParams = {};
var di = moment('2016-04-01', "YYYY-MM-DD").toArray();
var de = moment('2016-04-30', "YYYY-MM-DD").toArray();
searchParams['date_day'] = { $gte: new Date(di[0],di[1],di[2]), $lte: new Date(de[0],de[1],de[2]) };
searchParams['employee_id'] = 521
Reportico.find(searchParams).exec(function(err, employees) {
console.log('result')
if (err)return console.log('err',err);
console.log('employees',employees)
})
I'm trying to populate albums with images using userid sent through params but i end up with an empty array even though there is data in database.
"use strict"
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var Schema = mongoose.Schema
console.log('Welcome');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
mongoose.connect('mongodb://localhost/Datab');
var db = mongoose.connection;
db.once('open', function () {
console.log('Connected to Database');
});
//Schema for the data
var userSchema = new mongoose.Schema({
name: String
});
var albumSchema = new mongoose.Schema({
userid: String,
albumName: String,
albumDesc: String,
albumThumbnail: String,
imageCount: Number,
images: [{
type: Schema.Types.ObjectId,
ref: 'Image'
}]
});
var ImageSchema = new Schema({
album_id: {
type: String,
ref: 'Album'
},
imageName: String,
imageDescription: String,
imageURL: String,
likeCount: Number,
commentCount: Number
})
var Userdata = mongoose.model('Userdata', userSchema);
var Album = mongoose.model('Album', albumSchema);
var Image = mongoose.model('Image', ImageSchema);
//display of albums
app.get('/user/:user_id/photos', function (req, res) {
var user_id = req.params.user_id
Album.find({
userid: user_id
})
.populate('images')
.exec(function (err, albums) {
if (!err) {
return res.send({
status: {
error: 0,
message: "Successful"
},
data: {
albums: albums
}
})
} else
res.send('Unsuccessful');
})
});
app.listen(3000);
output:
{
"status": {
"error": 0,
"message": "Successful"
},
"data": {
"albums": []
}
}
why do i get an empty array as output?
Find in the mongo shell returns the following result
db.Album.find() {
"_id": ObjectId("529eed506c7b0a09b2204203"),
"albumDesc": "outings",
"albumName": "Photoclics1",
"albumThumbnail": "http: //192.168.71.250/uploads/cat1_thumb.jpg",
"imageCount": "1",
"images": ["529ef0016c7b0a09b2204205", "529ef0266c7b0a09b2204206"],
"userid": "529eec5a6c7b0a09b22041ff"
}
EDIT 2: This is the output that i expect to get
{
"error": {
"status": 0,
"message": "Successful"
},
"data": {
"albums": [
{
"albumName": "myclicks",
"albumID": "527102fdaed86d8807000001",
"albumDescription": "PhotosIclicked",
"albumThumbnailURL": "http: //192.168.71.250/uploads/image.jpg",
"imageCount": 2,
"images": [
{
"imageID": "527102fdaed86d8807000001",
"imageName": "mycat",
"imageDescription": "billy",
"imageURL": "http: //192.168.71.250/uploads/cat.jpg",
"imageThumbnailURL": "http: //192.168.71.250/uploads/cat_thumb.jpg",
"likeCount": 21,
"commentCount": 1
},
{
"imageID": "527102fdaed86d8807000001",
"imageName": "mycat",
"imageDescription": "billy",
"imageURL": "http: //192.168.71.250/uploads/cat1.jpg",
"imageThumbnailURL": "http: //192.168.71.250/uploads/cat1_thumb.jpg",
"likeCount": 21,
"commentCount": 1
}
]
}
]
}
The identifier stored in the images array is shown as a String in the console. Yet, the definition in the schema for the images field is shown as the type ObjectId. If you either fix the data to be an ObjectId or change the data type, the issue should be resolved.
You could fix the data in the MongoDB console by iterating through the collection and the array and converting each element to an ObjectId.