Meteor Simple-Schema Collection2 help needed for build nested schema - mongodb

I am trying to build a collection schema with Meteor Collection2.
Possible schema for my collection is :
{
items: {
id: Meteor.ObjectID
title: String,
Desc: String,
createdAt: new Date(),
creator: String,
invitedUsers: [String],
items2: [{
id: String,
pos: Number,
dur: Number,
startTime: Number,
endTime: Number,
duration: Number
items3: [{
id: String,
itemtype: String,
style: String,
items4: [{
id: String,
position: Number,
dome: String
}]
}]
}]
}
}
So how can I best build the Collection2 collection with the above nested schema and the best way to perform insert, update and remove queries on it.
Update:
So now as suggested by Andrei Karpushonak this is what I have got:
Item4 = new SimpleSchema({
position: {
type: Number
},
dome: {
type: String
}
});
Item3 = new SimpleSchema({
itemtype: {
type: String
},
style: {
type: String
},
items4: {
type: [Item4]
}
});
Item2 = new SimpleSchema({
pos: {
type: Number
},
dur: {
type: Number
},
startTime: {
type: Number
},
endTime: {
type: Number
},
duration: {
type: Number
},
items3 : {
type: [Item3]
}
});
Items = new Meteor.Collection2('items', {
schema : new SimpleSchema({
title: {
type: String
},
Desc: {
type: String
},
createdAt: {
type: new Date()
},
creator: {
type: String
},
invitedUsers: {
type: [String]
},
items2: {
type: [Item2]
}
})
});
So now I am trying to figure out how can I do the insert, update, remove operations on such a schema?
Do I do for individual schemas for as whole? An example will be very helpful.
Any help will be highly appreciated.
Thanks in Advance,
Praney

You have two options:
Create sub-schema:
item2 = new SimpleSchema({
id: String,
pos: Number
})
item1 = new SimpleSchema({
id: Meteor.ObjectID,
title: String,
items2: [item2]
});
Use dot notation:
item1 = new SimpleSchema({
id: String,
pos: Number,
"item2.id": String,
"item2.pos": String
});
I think first approach fits your model better, as you have array of objects as value of items2

Related

Return objects with ids matching the ids in another array mongoose

I'm having an issue running a query on my mongo DB. I want to return the objects that match the ids in the array of Ids. Not sure what I'm doing wrong everywhere online suggests doing it this exact way.
Mongoose Query
MovieSchema.statics.findKnownForMovies = function(ids) { return this.find({ id: { $in: [ids]}});
}
Mongoose Schema
const MovieSchema = new Schema({
adult: { type: Boolean },
category: {type: String},
id: { type: Number, required: true, unique: true },
poster_path: { type: String },
overview: { type: String },
release_date: { type: String },
original_title: { type: String },
genre_ids: [{ type: Number }],
original_language: { type: String },
title: { type: String },
backdrop_path: { type: String },
popularity: { type: Number },
vote_count: { type: Number },
video: { type: Boolean },
vote_average: { type: Number })
Array of ids
[ '577922', '487558', '429203' ]

Doubts on Mongodb query

I was designing a classifieds web app with the MERN stack. The MongoSchema is as shown below
const UserSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
books: [{
title: { type: String },
author: { type: String },
desc: { type: String },
price: { type: String },
image: { data: Buffer, contentType: String }
}],
date: {
type: Date,
default: Date.now
}
});
So all the other info except the books[] will be available after the initial sign-up, but what I want to do is to update the books array every time the user wishes to post a book for selling.
I'm planning to find the user by id, but I'm not quite sure how to add/append the info to the books array.
There are some answers to your question already in Stackoverflow.
For example:
Using Mongoose / MongoDB $addToSet functionality on array of objects
You can do something like this:
UserModel.js
const mongoose = require("mongoose");
const UserSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
books: [{
title: { type: String },
author: { type: String },
desc: { type: String },
price: { type: String },
image: { data: Buffer, contentType: String }
}],
date: {
type: Date,
default: Date.now
}
});
module.exports = User = mongoose.model("user", userSchema);
After that, in your router file you can try something like this for the books array:
const res = await User.updateOne({ email: 'gintama#bandainamco.com' }, {'$addToSet':{
'books':{
title: "Gintama: The Final",
author: "Sorachi",
desc: "Final Arc",
price: "44.99",
image: "src"
}}); //addToSet if you don't want any duplicates in your array.
OR
const res = await User.updateOne({ email: 'gintama#bandainamco.com' }, {'$push':{
'books':{
title: "Gintama: The Final",
author: "Sorachi",
desc: "Final Arc",
price: "44.99",
image: "src"
}}); //push if duplicates are okay in your array

Mongoose, proper way to update documents with deep nested schemas

I would like to know what would be the most efficient way to update a document that also has a nested schema in it. Normally I would just use Model.findByIdAndUpdate(id, updatedValues) ..., however if I try to do that with a document that has a deep nested schema I get this error: CastError: Cast to Embedded failed for value "{ _id: 5b1936aab50e727c83687797, en_US: 'Meat', lt_LT: 'Mesa' }" at path "title".
My Schemas look something like:
const titleSchema = new Schema({
en_US: {
type: String,
required: true
},
lt_LT: {
type: String,
default: null
}
})
const categorySchema = new Schema({
title: titleSchema
});
const ingredientSchema = new Schema({
title: {
type: titleSchema,
required: true
},
category: categorySchema,
calories: {
type: Number,
min: 0,
default: 0
},
vitamins: [String]
})
And I try to update like so:
{
title: {
en_US: 'Pork',
lt_LT: 'Kiauliena'
},
category: {
_id: '5b193a230af63a7e80b6acd8',
title: {
_id: '5b193a230af63a7e80b6acd7'
en_US: 'Meat',
lt_LT: 'Mesa'
}
}
}
Note, I get the new category object from a separate collection using just the category _id, but the final update object that goes into the findByIdAndUpdate() function looks like the one above.
The only workout I found is to remove the category from the updated values, update the document via findByIdAndUpdate(), get the updated document, assign the new category to it and save it.
It works just fine and all, but that requires two calls to the database, is it possible to do it in just one?
Try updating your schema like this:
const titleSchema = new Schema({
en_US: {
type: String,
required: true
},
lt_LT: {
type: String,
default: null
}
});
const ingredientSchema = new Schema({
title: {
type: titleSchema,
required: true
},
category: {
title: titleSchema
},
calories: {
type: Number,
min: 0,
default: 0
},
vitamins: [String]
});

How do you change a fieldName via mongoose method + change it in schema?

I have a schema like this:
const StorySchema = new Schema({
authorId: String,
title: String,
longUrl: String,
shortUrl: String,
comment: String,
category: String,
author: String,
date: { type: Date, default: Date.now, index: true },
votes: { type: Number, default: 1, index: true },
});
I want to change the votes field on the schema to be called votesCount and at the same time I want to actually change the schema.
Would I just do these in the same file?
const StorySchema = new Schema({
authorId: String,
title: String,
longUrl: String,
shortUrl: String,
comment: String,
category: String,
author: String,
date: { type: Date, default: Date.now, index: true },
votesCount: { type: Number, default: 1, index: true },
});
const StoryModel = mongoose.model('story', StorySchema);
StoryModel.update({}, { $rename: { votes: 'votesCount' } }, { multi: true, strict: false }, function(err, blocks) { });
Or do I not do this at all in the code? I have never dealt with database schema changes, so I'm not sure how / where to apply schema changes.
Make your changes in the Schema and the controller, as whatever name you use in your Schema field should also tally with the one in your Controller.
Eg.
const StorySchema = new Schema({
authorId: String,
title: String,
longUrl: String,
shortUrl: String,
comment: String,
category: String,
author: String,
date: { type: Date, default: Date.now, index: true },
votesCount: { type: Number, default: 1, index: true },
});
In your controller
let form = {
authorId:req.body.*****,
title:req.body.*****,
longUrl:req.body.*****,
shortUrl:req.body.*****,
comment:req.body.*****,
category:req.body.*****,
author:req.body.*****,
date:req.body.*****,
votesCount:req.body.*****
};
Note: the main point am trying to make here is that, the very name you used in the Schema should also the the same name you're gonna use for your controller.
I hope this is answers your question.
best use updateMany as
db.students.updateMany( {}, { $rename: { "nmae": "name" } } )
and change in your controller or directly replace previous field name to new field name where ever possible.
My Suggestion is better u first replace in controller or your project and if your project running in production update your new controller than u replace the field name using $rename

Collection schema for a quiz app in Meteor

I am creating a quiz app in Meteor.
What can a schema for a question look like? I am thinking of doing something like
const QuestionSchema = new SimpleSchema({
text: {
type: String,
},
correctAnswers: {
type: [Object],
},
'correctAnswers.$.text': {
type: String,
},
wrongAnswers: {
type: [Object],
},
'wrongAnswers.$.text': {
type: String,
},
});
but is it really smart? How can I save which answer some user chose?
As #gnerkus suggests it's simpler to have a correct boolean for each answer but each answer also needs an _id so that you reference it, for example which answers did the user select?
const AnswerSchema = new SimpleSchema({
_id: { type: String },
text: { type: String },
correct: { type: Boolean }
});
const QuestionSchema = new SimpleSchema({
text: { type: String },
answers: { type: [AnswerSchema] }
});
You'll need to define a separate schema for the answers.
var QuestionSchema = new SimpleSchema({
text: {
type: String
},
answers: [{
type: SimpleSchema.Types.ObjectId,
ref: 'AnswerSchema'
}]
});
var AnswerSchema = new SimpleSchema({
text: {
type: String
},
correct: {
type: Boolean
},
question: {
type: SimpleSchema.Types.ObjectId,
ref: 'QuestionSchema'
}
});
If you need to track the answers a user picked, you'll have to store them in the user instance:
var UserSchema = new SimpleSchema({
// Define other attributes here.
answers: [{
type: SimpleSchema.Types.ObjectId,
ref: 'AnswerSchema'
}]
});