How would I insert an object array into a schema?My current code:
const commentSchema = new mongoose.Schema({
user: String,
content: String
})
const Comment = mongoose.model("Comment", postSchema);
const postSchema = new mongoose.Schema({
title: String,
content: String,
comments: [Comment]
});
I'm getting the error:
Invalid schema configuration: `model` is not a valid type within the array `comments`.
How should I properly insert a list of objects? I am trying to make a list of comments under each post. Thank you.
This should work
const commentSchema = new mongoose.Schema({
user: String,
content: String
})
const postSchema = new mongoose.Schema({
title: String,
content: String,
comments: [commentSchema]
});
Here is a link for mongoose Subdocuments
Related
I am trying to create a document that has an array of another sub-document. I need to be able to export the sub-document for use elsewhere. I am using mongoose.model and this works, however, it creates an empty collection in my database that is just never used. How can I prevent this behavior?
Usage example:
const mongoose = require("mongoose");
const ChildSchema = new mongoose.Schema({
childName: String
});
const ParentSchema = new mongoose.Schema({
parentName: String,
children: [ChildSchema]
});
module.exports = {
Parent: mongoose.model("parent", ParentSchema),
Child: mongoose.model("child", ChildSchema)
}
You can do it with autoCreate and autoIndex options on your Schema:
const ChildSchema = new mongoose.Schema({
autoCreate: false,
autoIndex: false,
childName: String
});
const ParentSchema = new mongoose.Schema({
parentName: String,
children: [ChildSchema]
});
i have 2 Schema like bellow, i need to get 50 item from videoInfos which not have in userInfos.watched (this array content _id of videoInfos). Please use syntax like videoInfos.find().exce if you can.
const userSchema = new mongoose.Schema({
username:String,
password:String,
point: Number,
watched: Array,//content _id of videoInfos
running: Array,
});
const userInfos = mongoose.model('userInfos', userSchema);
//======================================================
const videoSchema = new mongoose.Schema({
owner:String,
videoID:String,
totalTime: Number,
totalView:Number,
finish: Number,
didFinish:Boolean,
});
const videoInfos = mongoose.model('videoInfos', videoSchema);
Since you are using mongoose you can achieve it like this.
Change the schema like this:
const userSchema = new mongoose.Schema({
username:String,
password:String,
point: Number,
watched: [{ type: Schema.Types.ObjectId, ref: 'videoInfos' }],
running: Array,
});
And query like this:
userInfos.find({}).populate('watched');
The watched array should be populated with videoInfo data.
For more, take a look at mongoose populate.
How to save .text of commentSchema as array format in the field of .comments in reviewSchema?
of course each review (post) has multiple comments.
// Connect to the db using mongoose
mongoose.connect("mongodb://localhost:27017/reviewdb")
// setting up model and schema for comment
var commentSchema = new mongoose.Schema( {
text: String,
user : String,
username: String,
date: Date
});
// setting up model and schema for reviews
var reviewSchema = new mongoose.Schema( {
text: String,
user : String,
username: String,
mydate: Date,
likes:{type: Number, "default":0, min:0},
comments:[commentSchema]
});
// setting up model for review
var Review = mongoose.model('review', reviewSchema);
// setting up model for comment
var comment = mongoose.model('comment', reviewSchema);
I'm trying to nest schemas using mongoose, but i got stuck and i don't really know why. Here is what i got.
My parent schema
const Comment = require("./Comment");
const BookSchema = new Schema({
_id: Number,
comments: [{ comment: Comment }],
ratings: [{ rate: Number }],
calculatedRating: Number
});
module.exports = Book = mongoose.model("book", BookSchema);
and child schema
const CommentSchema = new Schema(
{
userName: String,
rating: Number,
body: String,
submit_date: {
type: Date,
default: Date.now
}
},
{ _id: false }
);
module.exports = Comment = mongoose.model("comment", CommentSchema);
And with this setup im getting an error :
"TypeError: Invalid schema configuration: Model is not a valid type
at path comment."
I'm considering that i did something wrong with those exports but I'm not sure.
Your ./Comment should be:
const CommentSchema = new Schema(
{
userName: String,
rating: Number,
body: String,
submit_date: {
type: Date,
default: Date.now
}
},
{ _id: false }
);
module.exports = CommentSchema;
If you define as a new model as you did then it will create it's own collection and will be a new model instead of a sub document schema.
I want to do something like this:
var userSchema = new Schema({
local: localSchema,
facebook: facebookSchema,
twitter: twitterSchema,
google: googleSchema
});
But it seems that a Schema is not a valid SchemaType.
In the SubDocuments guide, they only give an example where the child schema is put inside of an array, but that isn't what I want to do.
var childSchema = new Schema({ name: 'string' });
var parentSchema = new Schema({
children: [childSchema]
})
It looks like you're just trying to create a sub object for each of those properties. You can accomplish this one of two ways.
Embedded in the schema itself
var userSchema = new Schema({
local: {
someProperty: {type: String}
//More sub-properties...
}
//More root level properties
});
Reusable object to be used in multiple schemas
//this could be defined in a separate module and exported for reuse
var localObject = {
someProperty: {type: String}
//more properties
}
var userSchema = new Schema({
local: localObject
});
var someOtherSchema = new Schema({
test: localObject
});