Nested maps in mongoose schema - mongodb

I'm currently creating a fitting mongoose schema for our new JSON format.
It is not very complex but I ran into the issue that certain values are not saved as array but rather as "normalized array" like this:
answers: [{value: 5, string: "abc"}, {value: 4, string: "def"}]
will be:
answers: {
1: {id: 1, value: 5, string: "abc"},
2: {id: 2, value: 4, string: "def"}
}
The Objects themselves can have nested "normalized arrays" as well.
For now I tried using mongoose type "Map" in the top-level-Schema like this:
answers: {
type: Map,
of: answer
}
Where "answer" is a separate mongoose.Schema itself.
But all I get is:
TypeError: Undefined type `Map` at `answers`
Did you try nesting Schemas? You can only nest using refs or arrays.
Why can't I just nest maps as expected? Is it even possible to project this "normalize array" structure in a mongoose schema, and if so, how?
Thank for reading!

I was in the same situation and it seems to work for me:
const ChildSchema = new Schema({
firstName: String,
});
const ParentSchema = new Schema({
name: String,
mapProperty: { type: Map, of: ChildSchema },
});
I do also get the validation triggered from mongoose on the ChildSchema so it seems to accept it just fine.
Hope it helps.

Related

How use mongoose init model with array of array of object [duplicate]

I have a Schema definition with a nested object that looks like this:
mongoose.Schema({
name: String,
messages: [{
type: String,
message: String
}]
});
Mongoose doesn't interpret this as I would like because there is a key named type, which conflicts with Mongoose's syntax for defining defaults, etc. Is there a way to define a key named "type"?
Oh, I remember this annoying problem, it took me ages to find out that the problem is that type is read by mongoose schema.
Just specify a type:String inside the type label
mongoose.Schema({
name: String,
messages: [{
type: {type: String},
message: String
}]
});

Accessing nested documents within nested documents

I'm having a problem that is really bugging me. I don't even want to use this solution I don't think but I want to know if there is one.
I was creating a comment section with mongodb and mongoose and keeping the comments attached to the resource like this:
const MovieSchema = new mongoose.Schema({
movieTitle: {type: String, text: true},
year: Number,
imdb: String,
comments: [{
date: Date,
body: String
}]
})
When editing the comments body I understood I could access a nested document like this:
const query = {
imdb: req.body.movie.imdb,
"comments._id": new ObjectId(req.body.editedComment._id)
}
const update = {
$set: {
"comments.$.body": req.body.newComment
}
}
Movie.findOneAndUpdate(query, update, function(err, movie) {
//do stuff
})
I then wanted to roll out a first level reply to comments, where every reply to a comment or to another reply just appeared as an array of replies for the top level comment (sort of like Facebook, not like reddit). At first I wanted to keep the replies attached to the comments just as I had kept the comments attachted to the resource. So the schema would look something like this:
const MovieSchema = new mongoose.Schema({
movieTitle: {type: String, text: true},
year: Number,
imdb: String,
comments: [{
date: Date,
body: String,
replies: [{
date: Date,
body: String
}]
}]
})
My question is how would you go about accessing a nested nested document. For instance if I wanted to edit a reply it doesn't seem I can use two $ symbols. So how would I do this in mongodb, and is this even possible?
I'm pretty sure I'm going to make Comments have its own model to simplify things but I still want to know if this is possible because it seems like a pretty big drawback of mongodb if not. On the other hand I'd feel pretty stupid using mongodb if I didn't figure out how to edit a nested nested document...
according to this issue: https://jira.mongodb.org/browse/SERVER-27089
updating nested-nested elements can be done this way:
parent.update({},
{$set: {“children.$[i].children.$[j].d”: nuValue}},
{ arrayFilters: [{ “i._id”: childId}, { “j._id”: grandchildId }] });
this is included in MongoDB 3.5.12 development version, in the MongoDB 3.6 production version.
according to https://github.com/Automattic/mongoose/issues/5986#issuecomment-358065800 it's supposed to be supported in mongoose 5+
if you're using an older mongodb or mongoose versions, there are 2 options:
find parent, edit result's grandchild, save parent.
const result = await parent.findById(parentId);
const grandchild = result.children.find(child => child._id.equals(childId))
.children.find(grandchild => grandchild._id.equals(grandchildId));
grandchild.field = value;
parent.save();
know granchild's index "somehow", findByIdAndUpdate parent with:
parent.findByIdAndUpdate(id,
{ $set: { [`children.$.children.${index}.field`]: value }});

Mongoose mixed SchemaType

I couldn't understand that for what purpose mongoose schemaType is used for. If someone could explain it will be helpful.
I'm have to reference another schema from a schema i want to know if we can get the details of all schema together when we do a findOne() on mongoose.
mixed schema means whatever you want the type to be. if you input a String, Number, Date, mongoose will let you do that. However according to documentation, mongoose ref does not work with mixed.
Note: ObjectId, Number, String, and Buffer are valid for use as refs.
if you use mixed, and ref it, you won't be able to query it back.
If you start all over(delete the database and reinsert again), use ObjectId instead of Mixed.
var storySchema = Schema({
author : { type: ObjectId, ref: 'Person' },
});
If you wish to retain old database, the best way is to change mixed to string
var storySchema = Schema({
author : { type: String, ref: 'Person' },
});

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'})

Understanding Relationships & Foreign Keys in Mongoose

In MongoDB/Mongoose, how do I define a relationship? I think there are a few ways I've seen, but I'm not sure I understand the differences or when do I use which. I am using Mongoose 3
I've defined Todo and TodoList model, where the relationship is obvious. So following the docs http://mongoosejs.com/docs/documents.html, I've defined classes like:
# Todo.coffee
mongoose = require "mongoose"
todoSchema = mongoose.Schema
name: String
desc: String
dueOn: Date
completedOn: Date
module.exports = mongoose.model "Todo", todoSchema
# TodoList.coffee
mongoose = require "mongoose"
Todo = require "./Todo"
todoListSchema = mongoose.Schema
name: String
todos: [Todo.schema]
module.exports = mongoose.model "TodoList", todoListSchema
Then I tried testing the classes:
list = new TodoList
name: "List 1"
todos: [
{ name: "Todo 1", desc: "Hello", dueOn: new Date(2012,10,1), completedOn: new Date(2012,10,2) }
{ name: "Todo 2" }
{ name: "Todo 3", desc: "Hello 2", dueOn: new Date(2012,10,6), completedOn: new Date(2012,10,2) }
{ name: "Todo 4" }
]
#list.todos.push { name: "Todo 5" }
console.log "List", list
list.save (err) ->
if !err
TodoList.find {}, (err, lists) ->
console.log "TODOS"
console.log lists.length, lists
done(err)
else
console.log "ERROR!!!"
done err
When I try to do Todo.find() I get nothing, so the Model (Todo.coffee) is kind of redundant? It looks like Todo are stored in TodoList, as a user I may not care, but I wonder what are the implications? Eg. will the document get too large? 1 TodoList with too many Todos? Does that matter? What if I allow nested Todos (not that I want to do itm just for understanding), is it better to store documents separately then? How do I do that, if I so desire, and when do I do it?
I saw another method, in Mongoose 2 actually. I don't know, if it is possible in 3. Something like using ObjectId instead of nested docs. Maybe thats to store it separately?
I'm still new to Node, Mongoose, and Mongo, but I think I can address at least part of your question. :)
Your current method is the same as I tried doing at first. Basically, it ends up storing it very similarly to this (written in JS, since I don't know CoffeeScript):
var todoListSchema = new mongoose.Schema({
name: String,
todos: [{
name: String,
desc: String,
dueOn: Date,
completedOn: Date
}]
});
I later found this method, which is what I was looking for, and I think what you were intending:
var todoListSchema = new mongoose.Schema({
name: String,
todos: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Todo' //Edit: I'd put the schema. Silly me.
}]
});
This stores an array of ObjectIds, which you can then load using Query#populate in Mongoose.
I don't know of the technical implications, but it makes more sense in my brain if I keep them separate, so that's what I'm doing. :)
Edit: Here is a some official docs that might be useful: http://mongoosejs.com/docs/populate.html