I know that if the size of the document grows, it will have a bad influence on Mongolia. So I avoid the following embedding document method.
However, many of the bulletin board basic examples use the following code.
var boardSchema = mongoose.Schema({
writer: String,
password: String,
title: String,
contents: String,
comments: [{
name: String,
memo: String,
date: {type: Date, default: Date.now}
}],
count: {type:Number, default: 0},
date: {type: Date, default: Date.now},
updated: [{contents: String, date:{type: Date, default: Date.now}}],
deleted: {type: Boolean, default: false}
});
In the case of bulletin boards and comments, is it possible to simply implement the embedded document method? Or did I misunderstand?
In addition, I implemented bulletin boards and comments in the following way.
var boardSchema = mongoose.Schema({
...
comments: [{type: ObjectId}],
...
});
var commentSchema = mongoose.Schema({
name: String,
memo: String,
date: Date
});
When creating in the comment schema, I implemented it by pushing the objectId as the bulletin board schema.
Related
I was wondering how can we add an array of string in a moongoose schema.
I have the following code but it is not working:
var message = new Schema({
topic: String,
content: String,
restriction:String,
sender:String,
reciever:String,
users:[String],
read:{type: String, default: 'no'},
like:{ type: Number, default: 0 },
created_at: {type: Date, default: Date.now}
});
I am talking about users. Can you help?
Cobbling together what you said in your comments and the main post, I can't help but think you're missing the modeling step of mongoose.
First you define the schema:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
var MessageSchema = new Schema({
topic: String,
content: String,
restriction:String,
sender:String,
reciever:String,
users:[String],
read:{type: String, default: 'no'},
like:{ type: Number, default: 0 },
created_at: {type: Date, default: Date.now}
});
Then you have to tell mongoose about it:
const Message = mongoose.model('Message', MessageSchema);
Then you can create an instance to put data into:
mongoose.connect('mongodb://localhost:27017/mydb'); // assuming that's a working mongo instance
let message = new Message();
message.users.push('Juliana');
message.save((e,u) => { console.log('New user saved!'); });
If I'm wrong, please post more info about what's not working.
var message = new Schema({
topic: String,
content: String,
restriction:String,
sender:String,
reciever:String,
users:[{
type: String
}],
read:{type: String, default: 'no'},
like:{ type: Number, default: 0 },
created_at: {type: Date, default: Date.now}
});
try this
var message = new Schema({
topic: String,
content: String,
restriction:String,
sender:String,
reciever:String,
users:[
{name: String}],
read:{type: String, default: 'no'},
like:{ type: Number, default: 0 },
created_at: {type: Date, default: Date.now}
});
I'm using mongoose to store three models of documents, sometimes I have to update references between then, for this I'm using mongoose-relationship plugin,
My need is reference then like this:
One customer have many schedules,
One costumer have many orders,
One order have many schedules
When I create an order I need to push schedules id's into order to reference then. But I can only reference one childPath per collection, my models are mapped like this;
Customers:
var CustomerSchema = new Schema({
name: {type: String, required: true},
email: {type: String, required: true},
shedules: [{ type:mongoose.Schema.Types.ObjectId, ref:"Schedule" }],
orders: [{ type:mongoose.Schema.Types.ObjectId, ref:"Order" }]
}
Schedules:
var ScheduleSchema = new Schema({
customer: {type:mongoose.Schema.Types.ObjectId, ref:"Customer", childPath:"shedules"}, //shedule id
order: {type:mongoose.Schema.Types.ObjectId, ref:"Order", childPath:"shedules"}, //order Id
sequence: {type: Number, default: 0},
creation_date: {type: Date, default: Date.now}
}
SheduleSchema.plugin(relationship, {relationshipPathName:['customer','order']});
Orders:
var OrderSchema = new Schema({
customer: {type:mongoose.Schema.Types.ObjectId, ref:"Customer", childPath:"order"},
shedules: [{type:mongoose.Schema.Types.ObjectId, ref:"Shedule" }],// <-- this field doesn't update.
price: {type: Number, default: 0}
}
OrderSchema.plugin(relationship, { relationshipPathName:'customer' });
I have some Schemas in mongoose as follows:
var tournamentSchema = new Schema({
name: {type: String},
teams: [teamSchema],
poules: [pouleSchema],
createdAt: {type: Date, default: Date.now}
})
var pouleSchema = new Schema({
teams: [teamSchema],
createdAt: {type: Date, default: Date.now}
})
var teamSchema = new Schema({
name: {type: String},
createdAt: {type: Date, default: Date.now}
})
I will push some teams in my tournamentSchema. When ready, I want to create the poules with the teams.
Is it possible to make a relation between the 'tournamentSchema.teams' and the 'tournamentSchema.poules'? I think it is not that difficult to push teams in the poules attribute, but when I want to change a name of a team, I want to change it in the poules also. Should I do this manually or is there some relation possible?
Or is this only possible with different Models?
Thank you in advance,
Ronald.
I think mongoose population can help you. Try something like this:
var tournamentSchema = new Schema({
name: {type: String},
teams: [teamSchema],
poules: [pouleSchema],
createdAt: {type: Date, default: Date.now}
})
var pouleSchema = new Schema({
teams: [teamSchema],
createdAt: {type: Date, default: Date.now}
})
var teamSchema = new Schema({
team: {
type: mongoose.Schema.Types.ObjectId,
ref: 'teamsSchema'
}
})
var teamsSchema = new Schema({
name: {type: String},
createdAt: {type: Date, default: Date.now}
})
So every object in your teams array referencing to ObjectId in teamsSchema, and when you change name of team it will automatically affects on teams arrays. Hope it will help you.
I have the following schema where I am trying to add an array of comments to my blog post schema, then inside the comments schema I need to add an array of pictures urls related to each specific comment. I've researched the web and found this link embedded documents to mongoose documentation, yet noticed that it is related to mongoose version 2.7 while we are currently at version 3.8. So was wondering if I am doing it right?, and if not can someone please help me by suggesting the best way for designing my blog post schema so that it includes the blog post array of comments as well as the array of pictures related to each comment. Thanks for your time and effort.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var pictures = new Schema({
picURL: String,
date: {
type: Date,
default: Date.now
}
});
var comments = new Schema({
subject: String,
body: String,
date: {
type: Date,
default: Date.now
},
pictures:[pictures]
});
var blogpost = new Schema({
title: String,
body: String,
date: {
type: Date,
default: Date.now
},
comments:[comments]
});
module.exports = mongoose.model('BlogPost', blogpost);
you have two common scenarios here how you would like to handle your information
Embedded document:
if you are likely to do more reads than writes it's recommended to follow this approach in this case your model could be like this:
var comments = new Schema({
subject: String,
body: String,
date: {
type: Date,
default: Date.now
},
pictures:[{
picURL: String,
date: {
type: Date,
default: Date.now
}
}]
});
and also your approach to me is ok and should run on 3.8 without potential issues.
Referenced document:
if you'll have more writes than reads you can different collections to split the information and make a reference to your objectId like:
var comments = new Schema({
subject: String,
body: String,
date: {
type: Date,
default: Date.now
},
pictures: [
{type: Schema.Types.ObjectId, ref: 'pictures'}
]
});
you'll need to separate each schema into it's own and delare a model for comments and images as well.
Either way both are valid if you ask me my personal preference is picking up embedded document approach.
EDIT:
this situation can be applied for N relationships between collections, I keep it simple for two relationships, but for you scenario could be like this:
var blogpost = new Schema({
title: String,
body: String,
date: {
type: Date,
default: Date.now
},
comments: [{
subject: String,
body: String,
date: {
type: Date,
default: Date.now
},
pictures:[{
picURL: String,
date: {
type: Date,
default: Date.now
}
}]
}]
});
referenced:
var blogpost = new Schema({
title: String,
body: String,
date: {
type: Date,
default: Date.now
},
comments:type: Schema.Types.ObjectId, ref: 'comments'}
});
hope that helps.
here's my photoSchema. it has a dbEntry object. should I create another dbEntry schema and ref it in the photoSchema. Or whatever I have is good enough? I am new to mongodb, try to figure out a correct way to create schema.
var photoSchema = new mongoose.Schema({
userId: ObjectId,
type: String,
createdOn: {type: Date, default: Date.now},
isDeleted: {type: Boolean, default: false},
isDownloaded: {type: Boolean, default: false},
dbFile: String,
dbEntry: {
revision: Number,
rev: String,
thumb_exists: Boolean,
bytes: Number,
modified: Date,
client_mtime: Date,
path: { type: String, unique: true}
}
});
It depends on how you plan to access the data. If you want to get the dbEntry object each time you query a photoSchema document, then what you have is probably the way to go.
If however, you're going to use dbEntry independent of the photoSchema document, then you should split it up and just keep a ref to it.
You can always fetch the ref using mongoose "populate"