These are chat archives for Automattic/mongoose

28th
Jan 2016
Hengki Sihombing
@hengkiardo
Jan 28 2016 10:43 UTC
@vkarpov15 how about if already have some document in several collections and then i wanna modify indexing on collection, how can do that with mongoose, since i have been using mongoose to creating schema and mongo document
Konstantin Baierer
@kba
Jan 28 2016 12:17 UTC
@aredo If you change the index, you'll need to re-index. Use e.g. the mongo client, remove the indexes and re-create them by calling ensureIndex or rely on the default Mongoose behavior (index on startup).
Karthik Muralidharan
@karthikmuralidharan
Jan 28 2016 16:40 UTC
Hey guys, I have created a schema for creating child documents for a parent schema. There are properties in the childschema that have default values set in case they aren’t defined when performing a create operation. But the child schema doc does not get created at all if the property in the parent doc pointing to the child schema is note defined. Is there a way where I can specify that the subdocument needs to be created with all the default properties specifed ?
Konstantin Baierer
@kba
Jan 28 2016 16:44 UTC
@karthikmuralidharan Can you make a gist of the relevant parts of your schemas? It's easier to grasp with concrete values
Karthik Muralidharan
@karthikmuralidharan
Jan 28 2016 16:45 UTC
sure
Karthik Muralidharan
@karthikmuralidharan
Jan 28 2016 16:52 UTC
I’m sorry I did not frame my question properly.
I have mandated certain fields to be required in the child schema
Chris Paradeise
@cparadeise
Jan 28 2016 16:54 UTC
@karthikmuralidharan where do you specify defaults?
Karthik Muralidharan
@karthikmuralidharan
Jan 28 2016 16:54 UTC
When I create an object from the Parent Model and in case I have not mentioned the price field while create the object from the model, no validation error is thrown based on the childSchema required fields
I mispoke about the defaults thing.
It’s actually the required parameter.
Chris Paradeise
@cparadeise
Jan 28 2016 16:55 UTC
gotcha, thinking
I don't have practice creating subdocuments from other schemas, so I am limited in helping your situation. gl
Konstantin Baierer
@kba
Jan 28 2016 16:58 UTC
@karthikmuralidharan You could add custom validation to parentSchema
Karthik Muralidharan
@karthikmuralidharan
Jan 28 2016 16:58 UTC
like { type : childSchema, required: true} ?
Konstantin Baierer
@kba
Jan 28 2016 16:59 UTC
Yes ... but you'd need to make it a SchemaType then. I remember that's possible but not sure how off the bat.
Karthik Muralidharan
@karthikmuralidharan
Jan 28 2016 17:01 UTC
Whoa, that works!!
Konstantin Baierer
@kba
Jan 28 2016 17:01 UTC
Nice :)
Chris Paradeise
@cparadeise
Jan 28 2016 17:01 UTC
gg
Konstantin Baierer
@kba
Jan 28 2016 17:01 UTC
You might want to google any mongoose plugins for currency/money though, maybe someone has already come up with a solution that handles edge cases and such.
Karthik Muralidharan
@karthikmuralidharan
Jan 28 2016 17:02 UTC
Funny, I don’t have to do that if I directly embed the childSchema definition in the parentSchema property.
@kba Ah got it.
Thanks :)
Konstantin Baierer
@kba
Jan 28 2016 17:03 UTC
Sure, happy to help :)
Raimo Pitkänen
@rapitkan
Jan 28 2016 17:30 UTC
Hello.
Chris Paradeise
@cparadeise
Jan 28 2016 17:41 UTC
Cheers
Renader
@Renader
Jan 28 2016 21:54 UTC

Hey fellas, i'm going my first steps with mongoose and node. Might anyone answer a question i have? Its about data modelling. I have 3 Things i need to store in my db. First is like "the product". Then i have to 2 different schema of forms that a user may attach to this product.

Later i'll have to add querys that show all products or all forms of type 1 or 2.

Is it Is it appropriate to store anything in one schema or do i create 3 schemas, 1 schema and 2 childschema? Thanks for any help i'm kinda stuck since i dont really know how to put my question into "google length"..

Chris Paradeise
@cparadeise
Jan 28 2016 22:07 UTC
db.products.find() = { "_id":obj(a), otherstuff...} {"_id":obj(b),otherstuff...}
db.productTypes.find({"productID":"obj(a)"}) = {"_id":obj(c),productID:obj(a),type:1,otherstuff..}
db.productTypes.find({"productID":"obj(b)"}) = {"_id":obj(d),productID:obj(b),type:2,otherstuff..}
like that?
that's only using 2 schemas tho
you could add productTypeInfo schema and reference the details from that
so productTypes would return the [typeInfoSchema] instead of 1 or 2
for the type field
Renader
@Renader
Jan 28 2016 22:16 UTC
i dont really get it @cparadeise - as i said - those are my first steps hehe. the product doesn't really need to be connected to the forms. With your approach i'd have to setup a product schema, but what exactly is schema 2?
Chris Paradeise
@cparadeise
Jan 28 2016 22:16 UTC
I got lost myself lol
the product, I only assume it had its own schema
my point with the second schema was to use the Primary key (_id) from the product and keep track of which type of product or form it is
and define the different types in a third?
But I also came from an rdb perspective, just started with mongo about 6months ago
Renader
@Renader
Jan 28 2016 22:20 UTC
Yeah its kinda tough for me, because i want to use relations like u just mentioned but on the other side i think "men this is nosql.. is it really the best approach to use relations here? " :smile:
Chris Paradeise
@cparadeise
Jan 28 2016 22:20 UTC
hehe it's a learning process, I'm trying to help out here because it helps me at times too lol
the embedded documents is where I am limited in my knowledge and have yet to practice it throughouly
Renader
@Renader
Jan 28 2016 22:21 UTC
It's great that u share your knowledge thanks for that. I'll just go with the references.
Chris Paradeise
@cparadeise
Jan 28 2016 22:22 UTC
np, sorry I couldn't solidify your solution
Renader
@Renader
Jan 28 2016 22:23 UTC
Dont worry this is great. i love the javascript world. so much gitter, stackoverflow and free tools.
Maybe after i've done the reference approach i'l try to do the same with the "subDocuments". Maybe one of the approaches will "feel" better to me then :smile: