Mongodb two collections aggregation - node.js

players schema:
var mongoose = require('mongoose'),Schema = mongoose.Schema;
var playerSchema = new mongoose.Schema({
name: String,
password: String,
country: [{ type: Schema.Types.ObjectId, ref: 'countries' }]
});
mongoose.model('players', playerSchema);
countries schema:
var mongoose = require('mongoose'),Schema = mongoose.Schema;
var countrySchema = Schema({
name: String,
isActive: Boolean
});
mongoose.model('countries', countrySchema);
And this is my node js find query:
router.route('/')
.get(function(req, res, next) {
mongoose.model('players').find().populate('country').exec(function (err, players) {
console.log(players);
if (err) {
return console.error(err);
} else {
res.format({
html: function(){
res.render('players/index', {
title: 'Players',
"players" : players
});
},
json: function(){
res.json(players);
}
});
}
});
});
I want the result set with countryId and name . But I got country is null.What to do ?

Related

How to pass nested arrays from backend to frontend using Mongoose in Node.js?

This is my MongoDB schema:
var partnerSchema = new mongoose.Schema({
name: String,
products: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Product'
}]
});
var productSchema = new mongoose.Schema({
name: String,
campaign: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Campaign'
}
]
});
var campaignSchema = new mongoose.Schema({
name: String,
});
module.exports = {
Partner: mongoose.model('Partner', partnerSchema),
Product: mongoose.model('Product', productSchema),
Campaign: mongoose.model('Campaign', campaignSchema)
}
And I'd like to send all documents (partner>product>campaign) to my View as a one object.
I know how to send partner with product ref. For example:
var campSchema = require('../model/camp-schema');
router.get('/partner-list', function (req, res) {
campSchema.Partner.find({}, function (err, partnerList) {
if (err) throw err;
res.json({ partnerList: partnerList });
}).populate('products');
});
And I can easily iterate at view in this way:
li(ng-repeat="product in partner.products")
a(href="#") {{ product.name }}
And here is the question. How can I pass ONE object as a document with partner, product and campaign. Because at the moment I have only partner and product in that object.
You can use this pattern to populate the nested campaign model:
var campSchema = require('../model/camp-schema');
router.get('/partner-list', function (req, res) {
campSchema.Partner
.find({})
.populate({
path: 'products',
model: 'Product',
populate: {
path: 'campaign',
model: 'Campaign'
}
}).exec(function(err, partnerList) {
if (err) throw err;
res.json({ partnerList: partnerList });
});
});

Why is mongoosastic populate / elastic search not populating one of my references? I'm getting an empty object

I have two models I'm attempting to reference. Style and Brand.
Brand populates with the needed object, but Style is always empty.
i've tried clearing cache / deleting indexes. With and without include_in_parent and type: 'nested'.
I feel it may have something to do with the specified es_type, etc.. not sure.
Product Schema:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Style = require('./style');
var Brand = require('./brand');
var mongoosastic = require('mongoosastic');
var ProductSchema = new mongoose.Schema({
name: { type: String, lowercase: true , required: true},
brand: {type: mongoose.Schema.Types.ObjectId, ref: 'Brand',
es_type:'nested', es_include_in_parent:true},
style: {type: mongoose.Schema.Types.ObjectId, ref: 'Style',
es_schema: Style, es_type:'nested', es_include_in_parent: true},
year: { type: Number }
});
ProductSchema.plugin(mongoosastic, {
hosts: [
'localhost:9200'
],
populate: [
{path: 'style'},
{path: 'brand'}
]
});
Product = module.exports = mongoose.model('Product', ProductSchema);
Product.createMapping(function (err,mapping) {
if(err){
console.log('error creating mapping (you can safely ignore this)');
console.log(err);
}else{
console.log('product mapping created!');
console.log(mapping);
}
});
var stream = Product.synchronize();
var count = 0;
stream.on('data', function(){
count++
});
stream.on('close', function(){
console.log('indexed whisks ' + count + " documents");
});
stream.on('error', function(){
});
style schema:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mongoosastic = require('mongoosastic');
var StyleSchema = new mongoose.Schema({
name: { type: String, lowercase: true , required: true},
});
Style = module.exports = mongoose.model('Style', StyleSchema);
Style.createMapping(function(err, mapping){
if(err) console.log('error w/ mapping : ', err);
console.log('mapping created');
console.log(mapping);
})
var stream = Style.synchronize();
var count = 0;
stream.on('data', function(){
count++
});
stream.on('close', function(){
console.log('indexed styles ' + count + " documents");
});
stream.on('error', function(){
});
search query:
exports.topSearch = function(req, res) {
console.log(req.body, "search product")
Product.search({query_string: {query: req.body.search}}, {from: req.body.fromNum,
size: req.body.size,
hydrate: req.body.hydrate
},
function(err, results) {
if (err) console.log('ERR', err);
if (results){
var data = results.hits.hits.map(function(hit) {
return hit
});
console.log('product data', data)
res.send(data);
}
else {
res.send({errmsg:'results not defined'})
}
});
};
When I query, I get this result in a hit:
_source:
{ name: 'Redemption White Rye Whiskey',
brand: [Object],
style: {},} },
regarding comment request:
Product being added to DB:
exports.create = function(req, res) {
Product.create(req.body, function(err, product) {
if (err) {
console.log('ERR', err)
};
res.send({
product: product
});
});
};
front / angular:
$scope.add = function () {
var prodStyle = JSON.parse($scope.selectedStyle);
$scope.product = $scope.product._id;
$scope.product.style = prodStyle._id;
console.log($scope.product.style, 'prod style');
Product.create($scope.product).then(function (res) {
res.data.product.style = { name: prodStyle.name };
$scope.products.push(res.data.product);
$scope.product = {};
$scope.selectedStyle = {};
});
};
I've got it working, but it differs much from the examples given on npm / github.
I had to remove the es_schema: Style, (as I had accidentally done for brand, which was why it worked). I had to add the es_type: "nested" / es_include_in_parent, which I gathered from elasticsearch and mongoosastic documentation.
I'm not sure this is intended, but it seems to work:
style: {type: mongoose.Schema.Types.ObjectId, ref: 'Style',
es_type:'nested', es_include_in_parent:true},
I now get : style: [Object] as needed, when I console.log results.hits .
Below is the example given in npm , which did not work for me:
var Comment = new Schema({
title: String
, body: String
, author: String
});
var User = new Schema({
name: {type:String, es_indexed:true}
, email: String
, city: String
, comments: {type: Schema.Types.ObjectId, ref: 'Comment',
es_schema: Comment, es_indexed:true, es_select: 'title body'}
})
User.plugin(mongoosastic, {
populate: [
{path: 'comments', select: 'title body'}
]
})

Mongoose one-to-many

can you explain me how to organize mongoose models to create one to many connections? It is needed keep separate collections.
suppose i have stores and items
//store.js
var mongoose = require('mongoose');
module.exports = mongoose.model('Store', {
name : String,
itemsinstore: [ String]
});
//item.js
var mongoose = require('mongoose');
module.exports = mongoose.model('Item', {
name : String,
storeforitem: [String]
});
Am i doing it in the right way?
And how to access pass data to arryas?
Here is the code yo enter name to item. But how to enter id to array of id's (itemsinstore)?
app.post('/api/stores', function(req, res) {
Store.create({
name: req.body.name,
}, function(err, store) {
if (err)
res.send(err);
});
})
You should use model reference and populate() method:
http://mongoosejs.com/docs/populate.html
Define your models:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var storeSchema = Schema({
name : String,
itemsInStore: [{ type: Schema.Types.ObjectId, ref: 'Item' }]
});
var Store = mongoose.model('Store', storeSchema);
var itemSchema = Schema({
name : String,
storeForItem: [{ type: Schema.Types.ObjectId, ref: 'Store' }]
});
var Item = mongoose.model('Item', itemSchema);
Save a new item into an existing store:
var item = new Item({name: 'Foo'});
item.save(function(err) {
store.itemsInStore.push(item);
store.save(function(err) {
// todo
});
});
Get items from a store
Store
.find({}) // all
.populate('itemsInStore')
.exec(function (err, stores) {
if (err) return handleError(err);
// Stores with items
});
You can do using the best practices with Virtuals.
Store.js
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const StoreSchema = new Schema({
name: {
type: String,
required: true
},
createdAt: {
type: Date,
default: Date.now
}
})
StoreSchema.virtual('items', {
ref: 'Item',
localField: '_id',
foreignField: 'storeId',
justOne: false // set true for one-to-one relationship
})
module.exports = mongoose.model('Store', StoreSchema)
Item.js
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const ItemSchema = new Schema({
storeId: {
type: Schema.Types.ObjectId,
required: true
},
name: {
type: String,
required: true
},
createdAt: {
type: Date,
default: Date.now
}
})
module.exports = mongoose.model('Item', ItemSchema)
StoreController.js
const Store = require('Store.js')
module.exports.getStore = (req, res) => {
const query = Store.findById(req.params.id).populate('items')
query.exec((err, store) => {
return res.status(200).json({ store, items: store.items })
})
}
Keep in mind that virtuals are not included in toJSON() output by default. If you want populate virtuals to show up when using functions that rely on JSON.stringify(), like Express' res.json() function, set the virtuals: true option on your schema's toJSON options.
// Set `virtuals: true` so `res.json()` works
const StoreSchema = new Schema({
name: String
}, { toJSON: { virtuals: true } });
Okay, this is how you define a dependancy:
var mongoose = require('mongoose');
module.exports = mongoose.model('Todo', {
name : String,
itemsinstore: [{ type: Schema.Types.ObjectId, ref: 'Item' }]
});
And make sure you have different names:
var mongoose = require('mongoose');
module.exports = mongoose.model('Item', {
name : String,
storeforitem: [String]
});
Keep an eye on Item in both cases.
And then you just want to pass the array of ObjectIDs in it. See more here: http://mongoosejs.com/docs/populate.html
Try this:
Store.findOne({_id:'5892b603986f7a419c1add07'})
.exec (function(err, store){
if(err) return res.send(err);
var item = new Item({name: 'Foo'});
item.save(function(err) {
store.itemsInStore.push(item);
store.save(function(err) {
// todo
});
});

Retrieve Documents containing a subdocument in a REST API

I am trying to retrieve a list of documents that contain a sub doc to be listed on a web application.
I have my models setup as such:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var storeSchema = new mongoose.Schema({
name: String,
address: String,
phone: String,
webUrl: String,
coords: {type: [Number], index: '2dsphere'}
});
var reviewSchema = new mongoose.Schema({
user: {type: String, required: true},
store: { type: Schema.ObjectId, ref: 'Store' },
review: {type: String},
tags: [String]
});
mongoose.model('Review', reviewSchema);
mongoose.model('Store', storeSchema);
And the api controller setup as such:
var mongoose = require('mongoose');
var Game = mongoose.model('Review');
var sendJsonResponse = function(res, status, content) {
res.status(status);
res.json(content);
};
module.exports.gamesListByDistance = function(req, res) {
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]
};
var geoOptions = {
spherical: true,
maxDistance: theEarth.getRadsFromDistance(maxDistance),
num: 10
};
Review.geoNear(point, geoOptions, function(err, results, stats) {
console.log('Geo Results', results);
console.log('Geo stats', stats)
if (err) {
console.log('geoNear error:', err);
sendJsonResponse(res, 404, err);
} else {
results.populate(results, {path:'store', select:'name coords'}, function(err,reviews) {
if (err) {
sendJsonResponse(res, 400, err);
} else {
games = buildReviewsList(req, res, results, stats);
sendJsonResponse(res, 200, reviews);
}
});
}
});
};
var buildReviewsList = function(req, res, results, stats) {
var reviews = [];
results.forEach(function(doc) {
reviews.push({
distance: theEarth.getDistanceFromRads(doc.dis),
store: doc.obj.store.name,
status: doc.obj.status,
tags: doc.obj.tags,
_id: doc.obj._id
});
});
return reviews;
};
But am getting:
TypeError: undefined is not a function
What is the proper way to populate these subdocs and return the list of reviews to be consumed with a Web Application?
Why do you have :
var Game ?
should be var Review.

Packing data with mongoose

I have 3 files with different schema. User has many notebooks and notebooks has many notes. Example of schemas :
UserSchema:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var User = new Schema({
username: { type: String, require: true, index: { unique: true }, trim: true},
password: { type: String, require: true, select: true },
age: { type: Number, min: 0 },
firstname: String,
secondname: String,
token: String,
role: String,
city: String,
rememberMe: Boolean
});
module.exports = mongoose.model('User', User);
NotebookSchema:
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
Note = require('./note'),
User = require('./user');
var NoteBook = new Schema({
creator: { type:Schema.ObjectId, ref:"User"},
name: String,
description: String
});
NoteBook.methods.getAllNotes = function(cb) {
Note.find({notebook: this}, function(err, noteList){
cb(noteList);
});
};
module.exports = mongoose.model('NoteBook', NoteBook);
NoteSchema:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var NoteSchema = new Schema({
notebook: { type: Schema.Types.ObjectId, ref: 'NoteBook'},
name: String,
description: String,
content: String
});
module.exports = mongoose.model('Note', NoteSchema);
I get in request userId and I need to pack json all this line of data. User with Notebooks and Notebooks with note in one json. I triyed something like this:
function getTree(req, res) {
var data = [];
User.findOne({_id: req.body.userId}, function(err, user) {
NoteBook.find({creator: user._id}, function(err, notebookList) {
for (var idx in notebookList) {
Note.find({notebok: notebookList[idx]._id}, function(err, noteList) {
var children = [];
for (var noteIdx in noteList) {
children.push({
'text': noteList[idx].name,
'a_attr' : {
'data-node-type': 'note',
'data-node-id': noteList[idx]._id,
},
});
}
data.push({
'text': notebookList[idx].name,
'a_attr' : {
'data-node-type': 'notebook',
'data-node-id': notebookList[idx]._id,
},
'children': children
});
});
}
res.json({ tree: data });
});
});
}
but it doesn't work.
var async = require("async");
function getTree(req, res) {
var data = [];
User.findOne({_id: req.body.userId}, function(err, user) {
NoteBook.find({creator: user._id}, function(err, notebookList) {
async.forEach(notebookList, function(notebook, callback){
Note.find({notebok: notebook._id}, function(err, noteList) {
var children = [];
for (var noteIdx in noteList) {
children.push({
'text': noteList[idx].name,
'a_attr' : {
'data-node-type': 'note',
'data-node-id': noteList[idx]._id,
},
});
}
data.push({
'text': notebookList[idx].name,
'a_attr' : {
'data-node-type': 'notebook',
'data-node-id': notebook._id,
},
'children': children
});
});
}, function(err){
res.json({ tree: data });
});
});
});
}

Resources