How to query on Embedded document within an array - MongoDB? - mongodb

I've trying to query on an embedded document i've created that has the following model: a user has a facebook account and a business (emprendimiento). The 'emprendimiento' has categorias_asociadas, that is an empty array that will receive its value(s) from a form tag (Buy, Sell or Invest).
I would like to query all Users that have an emprendimiento that have a catogorias_asociadas that is either buy, sell or invest.**
I'm looking for something like this:
Users.find({'emprendimiento.categorias_asociadas': {$all: 'Buy'}})
var userSchema = mongoose.Schema({
facebook: {
id: String,
token: String,
email: String,
name: String,
photo: String
},
emprendimiento: {
nombre:String,
slogan:String,
logo:String,
web:String,
categorias_asociadas: [],
about: String,
quevende: String,
ubicacion: String,
descripcion: String
}
});
I've accomplished something similar before when I had the 'emprendimiento' in another model:
var emprendimientoSchema= new mongoose.Schema({
nombre:String,
slogan:String,
logo:String,
categorias_asociadas: []
});
var Emprendimiento = mongoose.model('Emprendimiento', emprendimientoSchema)
By doing this query I was able to filter all emprendimiento that have compraVenta (Buy) in its categorias_asociadas.
Emprendimiento.find({categorias_asociadas:{$all:["buy"]}}, function (err, compraventa) {
if (err) {
console.log(err)
}
else {
do something here
}
});
Thanks in advance :)

This worked well :)
Users.find({'emprendimiento.categorias_asociadas': {$all: ['Buy']}})

Related

Only first array element gets updated in nested document Mongoose

This is my schema:
let userSchema = new mongoose.Schema({
id: String,
displayName: String,
displayImage: String,
posts: [
{
url: String,
description: String,
likes: [String],
comments: [
{ content: String, date: String, author: { id: String, displayName: String, displayImage: String } }
]
}
]
});
I am trying to edit the elements in the comment array and am already aware that I should have made two separate schemas due to MongoDB's limited functionality when it comes to manipulating double nested docs.
But anyhow, this seems to work for me, remember I am trying to edit the contents of a particular comment in the comment array.
controller.editComment = (req, res, next) => {
User.findOne(
{ id: req.query.userid, 'posts._id': req.params.postid },
{ 'posts.$.comments._id': req.body.commentID }
)
.exec()
.then((doc) => {
let thisComment = doc.posts[0].comments.filter((comment) => { return comment._id == req.body.commentID; });
thisComment[0].content = req.body.edited;
doc.save((err) => { if (err) throw err; });
res.send('edited');
})
.catch(next);
};
This works, however it ALWAYS only updates the comments of the very first post, regardless of which comment I edit. But please keep in mind that thisComment[0].content, if console.logged, will always show the correct content of the correct comment under the correct post. However, upon doc.save(err) is where I assume the problem is happening.
Any direction is GREATLY appreciated, I really can't see what seems to be the problem.

Why I can't get document via mongoose?

I'm getting document via MongoDB Shell:
db.page_about_love.find()
But I can't get document via mongoose. What is wrong?
mongoose.connect(db_uri);
var loveSchema = new mongoose.Schema({
title: String,
content: String,
tag: String
});
mongoose.model('page_about_love', loveSchema);
var about = mongoose.model('page_about_love');
about.find(function (err, love) {
if (err) return console.error(err);
console.log(love);
});
Test output:
[]
To prevent Mongoose generating a collection name to use, you should be explicit and pass which collection name it should use:
var loveSchema = new mongoose.Schema({
title: String,
content: String,
tag: String
}, { collection : 'page_about_love' });
Otherwise, Mongoose will apply the utils.toCollectionName() function to the model name to determine the collection name, which in your case would yield page_about_loves (notice the pluralization).
More information here.

Mongoose populate() returning empty array

so I've been at it for like 4 hours, read the documentation several times, and still couldn't figure out my problem. I'm trying to do a simple populate() to my model.
I have a User model and Store model. The User has a favoriteStores array which contains the _id of stores. What I'm looking for is that this array will be populated with the Store details.
user.model
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var UserSchema = new Schema({
username: String,
name: {first: String, last: String},
favoriteStores: [{type: Schema.Types.ObjectId, ref: 'Store'}],
modifiedOn: {type: Date, default: Date.now},
createdOn: Date,
lastLogin: Date
});
UserSchema.statics.getFavoriteStores = function (userId, callback) {
this
.findById(userId)
.populate('favoriteStores')
.exec(function (err, stores) {
callback(err, stores);
});
}
And another file:
store.model
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var StoreSchema = new Schema({
name: String,
route: String,
tagline: String,
logo: String
});
module.exports = mongoose.model('Store', StoreSchema);
After running this what I get is:
{
"_id": "556dc40b44f14c0c252c5604",
"username": "adiv.rulez",
"__v": 0,
"modifiedOn": "2015-06-02T14:56:11.074Z",
"favoriteStores": [],
"name": {
"first": "Adiv",
"last": "Ohayon"
}
}
The favoriteStores is empty, even though when I just do a get of the stores without the populate it does display the _id of the store.
Any help is greatly appreciated! Thanks ;)
UPDATE
After using the deepPopulate plugin it magically fixed it. I guess the problem was with the nesting of the userSchema. Still not sure what the problem was exactly, but at least it's fixed.
I think this issue happens when schemas are defined across multiple files. To solve this, try call populate this way:
.populate({path: 'favoriteStores', model: 'Store'})

Is it possible to extract only embedded documents from a Model in Mongoose?

I'd like to run a query on a Model, but only return embedded documents where the query matches. Consider the following...
var EventSchema = new mongoose.Schema({
typ : { type: String },
meta : { type: String }
});
var DaySchema = new mongoose.Schema({
uid: mongoose.Schema.ObjectId,
events: [EventSchema],
dateR: { type: Date, 'default': Date.now }
});
function getem() {
DayModel.find({events.typ : 'magic'}, function(err, days) {
// magic. ideally this would return a list of events rather then days
});
}
That find operation will return a list of DayModel documents. But what I'd really like is a list of EventSchemas alone. Is this possible?
It's not possible to fetch the Event objects directly, but you can restrict which fields your query returns like this:
DayModel.find({events.typ : 'magic'}, ['events'], function(err, days) {
...
});
You will still need to loop through the results to extract the actual embedded fields from the documents returned by the query, however.

Mongoose: How to model a foreign key/inverse relationship?

I am using Mongoose to model Person and Transaction collections, where each Transaction will have references to two different Person instances:
var TransactionSchema = new Schema({
, amount : { type: Number, required: true }
, from : { type: ObjectId, required: true }
, to : { type: ObjectId, required: true }
, date : Date
});
var PersonSchema = new Schema({
name : { type: String, required: true }
, transactions : [ObjectId]
});
I'd like each Person to have a collection of all the Transactions that they are either the to or from value for. So far, this is the best way I've been able to figure out how to do it:
TransactionSchema.pre('save', function(next, done) {
var transaction = this;
Person.findById(this.to, function (err, person) {
person.transactions.push(transaction);
person.save();
});
Person.findById(this.from, function (err, person) {
person.transactions.push(transaction);
person.save();
});
next();
});
This seems excessive. Is there a better way to do it, or am I trying to use MongoDB too much like a relational database? Instead of having a collection of Transactions associated with each Person instance, should I just be querying the Translation collection directly?
Thank you.
You've got to think more on the queries you are going to execute on the database when you design the MongoDB schema.
Try to duplicate data for speed and reference it for integrity. What does that mean?
Well, for example when you make a query for a Transaction, I guess you don't need all the user details from the first time no? (do you need the user's email, location when displaying info on a Transaction?)
I think you just probably need the user id and the username, so you should do something like this:
var TransactionSchema = new Schema({
, amount : { type: Number, required: true }
, from : {
user_id: {
type: ObjectId
, required: true
}
, username: {
type: String
, required: true
}
}
, to : {
user_id: {
type: ObjectId
, required: true
}
, username: {
type: String
, required: true
}
}
, date : Date
});
So instead of doing 3 queries for the page displaying the Transaction details (one for the transaction and 2 additional queries for the usernames), you'll have just one.
This is just an example, you could apply the same logic for the User schema, depending on what you're trying to achieve.
Anyway I don't think your middleware is ok, since you are not checking for errors there (you are always calling next no matter what). This is how I would write the middleware (didn't test, but the idea is important):
TransactionSchema.pre('save', function(next, done) {
var transaction = this;
Person.where('_id').in([this.to, this.from]).run(function (err, people) {
if (people.length != 2) { next(new Error("To or from doesn't exist")); return; }
Step(
function save_to() {
people[0].transactions.push(transaction);
people[0].save(this);
},
function save_from(err) {
if (err) { next(err); return; }
people[1].transactions.push(transaction);
people[1].save(this);
},
function callback(err) {
next(err);
}
);
});
});
In the code above I'm using the Step library for flow control and I'm only using one query instead of two (when searching for "to" and "from").