Default values not adding to mongoose Model - javascript

So this is an example of my schema I have for a user.
id: String,
email: String,
slug: {
type: Object,
phrase: {type: String, default: null},
},
When I want to define a new user and save that user, I would do the following;
const newUser = new User({
id: 123,
username: "CoolUser",
email: "BillGates#google.com"
});
newUser.save();
But this does not save the "slug" object, It was my understanding, that since I a default value for it, it would auto populate with that default value. What can I do to make it auto generate without having to define the whole schema again when saving a user?

You should add default for slug property, and not for his sub-property. Try changing your schema like this:
slug: {
type: {
phrase: { type: String },
},
default: {
phrase: null
},
},

try this:
const subschema = new Schema({
phrase: {type: String, default: null},
}, { _id: false });
and in your original schema:
id: String,
email: String,
slug: {
type: subschema,
default: () => ({})
},
this should do the trick.

const User = new Schema({
id: String,
about: {
bio: String,
location: String,
website: String,
discord: String,
twitter: String,
default: {
bio: "This is a default bio.",
location: "",
website: "",
discord: "",
twitter: "",
}
}
})
Ok here is the example, I removed some stuff from it just for ease, Basically as you can see I want bio to default to: This is a default bio.
But instead, I get the following error:
throw new TypeError(`Invalid schema configuration: \`${name}\` is not ` +
^
TypeError: Invalid schema configuration: `This is a default bio.` is not a valid type at path `about.default.bio`. See bit ly / mongoose-schematypes for a list of valid schema types.

Related

findOneReplace replacment issue

I want to find a document in my db and replace it with a document that has a new name and new key.
Here is my Schema
const Schema = mongoose.Schema;
const vampireSchema = new Schema({
name: { type: String, required: true },
title: String,
hair_color: {type: String, default: "blonde" },
eye_color: String,
dob: Date,
loves: [String],
location: String,
gender: String,
victims: {type: Number, min: 0}
});
const Vampire = mongoose.model("Vampire", vampireSchema);
module.exports = Vampire;
Here is my executable code
Vampire.findOneAndReplace( { name: "Claudia" }, { name: "Eve", portrayed_by: "Tilda Swinton" }, (err, vamp) => {
if(err){
console.log(err)
}
else{
console.log(vamp)
}
db.close()
})
There are two issues that I can see.
First, you should pass null as the third argument in your findOneAndReplace call. This will set the options to null and should get your code running. This is in my opinion a strange behavior of mongoose.
Vampire.findOneAndReplace(
{ name: "Claudia" },
{ name: "Eve", portrayed_by: "Tilda Swinton" },
null,
(err, vamp) =>
{
if(err){
console.log(err)
}
else{
console.log(vamp)
}
db.close()
})
Secondly, I would recommend adding the portrayed_by to the schema, otherwise, that field will not be in the newly created document. Therefore, I would adjust your schema that way:
const vampireSchema = new Schema({
name: { type: String, required: true },
title: String,
hair_color: {type: String, default: "blonde" },
eye_color: String,
dob: Date,
loves: [String],
location: String,
gender: String,
victims: {type: Number, min: 0},
portrayed_by: String
});

Why mongoose populate doesn't work when populating an array?

I have 2 schemas:
const mongoose = require('mongoose');
const PinSchema = new mongoose.Schema({
title: String,
content: String,
image: String,
latitude: Number,
longitude: Number,
author: {
type: mongoose.Schema.ObjectId,
ref: "User"
},
comments: [
{
text: String,
createdAt: {
type: Date,
default: Date.now,
author: {
type: mongoose.Schema.ObjectId,
ref: "User"
}
}
}
]
}, { timestamps: true });
module.exports = mongoose.model("Pin", PinSchema);
and
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
name: String,
email: String,
picture: String
});
module.exports = mongoose.model("User", UserSchema);
As you can see author field in Pin is the same as the _id in User schema.
I then try to populate the comments author field in the Pin schema like this:
const pinUpdated = await Pin.findOneAndUpdate(
{ _id: pinId },
{ $push: { comments: "some comment" } },
{ new: true }
).populate("author")
.populate("comments.author");
however the result object has author field set to null so population doesn't work.
I'm not against doing this with native mongo syntax using $lookup but in my case it's not just looking up an array it's looking up a field of an objects array:
db.pins.aggregate([
{
$lookup:
{
from: "users",
localField: "comments._id", // this won't work
foreignField: "_id",
as: "authorOutput"
}
}
])
what am I missing in populate()?
It looks like your author field in the comments array is nested inside the createdAt object, which is likely unintentional. Changing PinSchema to the following (closing curly brace before author) should fix it:
const PinSchema = new mongoose.Schema({
...
comments: [
{
text: String,
createdAt: {
type: Date,
default: Date.now,
},
author: {
type: mongoose.Schema.ObjectId,
ref: "User"
}
}
]
}, { timestamps: true });

Converting to Mongo/mongoose from Mysql with relations

In a new project I started with MySQL but I want to convert it to mongodb / moongoose.
My pain here is the different relations I have in MySQL.
F.x. I have have users table
uid | firstname | lastname | email
and i housetable
hid | address | zipcode | city | ownerid
where ownerid is a user in the users table
I am a little unsure how I should design me schemas in mongoose to have an option a bit like the MySQL one.
My current user schema is like this
const userSchema = new mongoose.Schema({
firstname: String,
lastname: String,
email: String,
phone: Number,
password: String,
address: String,
zipcode: Number,
city: String,
bankname: String,
bankregno: Number,
bankaccno: Number,
userconfirmed: Boolean,
confirmtoken: String,
resettoken: String,
resettokenexpires: Date,
acceptmarketing: Boolean,
lastloggedin: Date,
disabled: Boolean
})
You can add a field for an array of houses in your userSchema to break it up and allow a one to many relation. You can do this by using subdocuments.
This could look something like this.
const houseSchema = new mongoose.Schema({
address: String,
zipcode: Number,
city: String,
})
const userSchema = new mongoose.Schema({
firstname: String,
lastname: String,
email: String,
phone: Number,
password: String,
bankname: String,
bankregno: Number,
bankaccno: Number,
userconfirmed: Boolean,
confirmtoken: String,
resettoken: String,
resettokenexpires: Date,
acceptmarketing: Boolean,
lastloggedin: Date,
disabled: Boolean,
houses: [houseSchema],
})
you can use mongoose populate, like this
const User = new mongoose.Schema({
House: { type: mongoose.Types.Schema.ObjectId, ref: "House" }
});
const House = new mongoose.Schema({
Number,
Road: String
});
(async () => {
const users = await User.find({}).populate("House");
// users = [
// {
// _id: 373890hfjklfhjkfh,
// House: {
// _id: dhkdhdlhjkld,
// Number: 378930,
// Road: fhjdlhdjkld
// }
// }
// ]
})();
If you are looking to relate both the schemas, this might help you. If you want more info, https://mongoosejs.com/docs/2.7.x/docs/populate.html
const User = mongoose.model('User', new mongoose.Schema({
firstname: {
type: String,
},
lastname: {
type: String,
},
email: {
type: String
}
}));
const Address = mongoose.model('Address', new mongoose.Schema({
owner: {
type: Schema.Types.ObjectId,
ref: 'User'
}
//rest of your address schema
}))

Mongodb mongoose node js schema relation

hello i am new on mongodb and node js i have a question
here is my product schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const categorySchema = require('./category');
const ProductSchema = new Schema({
country: {
type: String,
required: [true, "country can't be null"]
},
city: {
type: String,
default: ""
},
name: {
type: String,
required: [true, "name can't be null"]
},
measureValue: {
type: Number,
default: 0
},
minPrice: {
type:Number,
required: [true, "minPrice can't be null"],
min: [1,"minPrice must be at least 1"]
},
maxPrice: {
type:Number,
required: [true, "maxPrice can't be null"],
min: [1,"maxPrice must be at least 1"]
},
photoUrl: {
type:String,
default: ""
},
explanation: {
type: String,
default: ""
},
category: [categorySchema.schema],
userID: {
type: String,
required: [true,"userid cant be null"]
},
isActive: {
type: Boolean,
default: true
},
createdDate: {
type: Date,
default: Date.now
},
deletedDate: {
type:Date
}
})
and here is my category schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const CategorySchema = new Schema({
name: {
type: String,
required: [true, "name can't be null"]
},
createdDate: {
type: Date,
default: Date.now
},
deletedDate: {
type:Date
}
})
i need to do this;
every product data must be have category
if one day,one category's name changed then every product that relation with that category must changed
i am trying to set category id to product schema and when i fetch the data it must be comes every product with category name as json
i am really confused if you help me i'd be really thankful
You can set up your category as :
category: {
type: mongoose.Schema.Types.ObjectId,
ref: 'category' //your model name
}
You can wrap it into an array and name it categories if you want multiple categories.
Then when you get the data, you will have to execute new Product().populate('category') to get retrieve the category data instead of just returning the category ObjectId.

MongoDB - Mongoose: document structure/architecture

I have a relatively simple question, however, I keep getting stuck, over and over again. I think this is the fourth time I have tried to re-structure my documents to try and get the desired result.
I want to create a database in mongoose with the following structure:
A User has many groups, posts and comments. The groups, posts and comments all belong to a User. A post can belong to many groups, or none. And a comment belongs to a certain post.
Here is what I have so far:
UserSchema
var userSchema = mongoose.Schema({
local : {
email : String,
password : String,
username : String,
created: {type:Date, default: Date.now}
}
});
PostSchema
var PostSchema = new Schema({
url: String,
highlighted: String,
comment: String,
image: String,
timeStamp: String,
description: String,
title: String,
created: {type:Date, default: Date.now},
private: Boolean,
favorite: Boolean,
readlater: Boolean,
postedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
comments: [{
text: String,
postedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
}],
groups: [{
name: String,
postedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
}]
});
groupSchema
var GroupSchema = Schema({
name : String,
created: {type:Date, default: Date.now},
postedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
});
Although I may be wrong on this assumption, I think I have the post to user and comment to post relationship just the way it should be. Please correct me if I am wrong. The main problem I am having right now is with my group to post relationship. Currently, there is no relationship at all. As far as I can, it is almost as if the group belongs to the post, instead of the other way around. The only other way I can think to structure the group would be like so:
var GroupSchema = Schema({
name : String,
created: {type:Date, default: Date.now},
postedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
posts: [{
title: String,
Url: String,
comments: [{
text: String
}]
etc....
}]
});
The only problem I can see in the structure above is that the post will be required to be part of a group, which I want to be optional. So as far as I can tell, this will not work either.
If there is any advice you can provide in regards to how this should be structured, it would help me greatly.
Thank you in advance.
Your best bet is to embed your groups and comment schemas inside of your post document. For example,
var GroupSchema = Schema({
name: String,
created: { type:Date, default: Date.now },
});
var CommentSchema = Schema({
text: String,
postedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
});
var PostSchema = new Schema({
url: String,
...,
postedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
comments: [CommentSchema]
groups: [GroupSchema]
});
Now, if you are looking for posts with a particular group, you can build a query as follows. You can build more complicated queries than this however.
Post.elemMatch("groups", {name: {$in: ["group1", "group2"]}}).then(...)

Categories