Collection is chats.
Model name is Chat.
import {model, Schema} from 'mongoose';
const ChatSchema = new Schema(
{
username: {type: String, required: true, index: true},
message: {type: String, required: true},
}
);
export default model('Chat', ChatSchema);
I can get the model via mongoose.model('Chat') just fine.
But, is there a way to get the Chat model by the collection name chats?
You need to set the collection name in schema too:
const ChatSchema = new Schema(
{
username: {type: String, required: true, index: true},
message: {type: String, required: true},
},
{ collection: 'chats' } // <-- here
);
Reference
Related
i have two schemas: Item and Category. I want to use Category in Items, how can i merge them? Here is the code:
Category:
const mongoose = require('mongoose');
const categorySchema = mongoose.Schema({
name: {type: String, required: true},
description: {type: String, required: true},
city: {type: String, required: true},
});
var categoryData = mongoose.model("categoryData", categorySchema);
module.exports = categoryData;
Item:
const mongoose = require('mongoose');
const categSchema = require("./category.js")
const itemSchema = mongoose.Schema({
name: {type: String, required: true},
created: {type: Date, required: true, unique: true},
category: [categSchema],
quantity: {type: Number, required: true}
});
var itemData = mongoose.model("itemData", itemSchema);
module.exports = itemData;
And the error that i get is "TypeError: Invalid schema configuration: model is not a valid type within the array category."
What am I doing wrong?
Instead of module.exports = categoryData
Do the following:
module.exports = categorySchema.
I have two tables: Post and User and I want to reference the User name in the Post table.
I can reference the ID but I also need the name.
User model:
const userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
})
module.exports = mongoose.model("User", userSchema)
Post model:
const { ObjectId } = mongoose.Schema.Types
const postSchema = new mongoose.Schema({
title: {
type: String,
required: true,
},
author: {
type: ObjectId,
ref: "User",
},
})
module.exports = mongoose.model("Post", postSchema)
In the doc they said:
Note: ObjectId, Number, String, and Buffer are valid for use as refs.
However, you should use ObjectId unless you are an advanced user and
have a good reason for doing so.
However I couldn't find an example on how I can do this.
I have the two mongoose schemas where one schema reference the other. For example I have
const loginsSchema = new mongoose.Schema({
displayName:{
type:String,
required:true,
},
serviceId:{
required:true,
type:String,
unique:true
}
})
const logins = mongoose.model('logins',loginsSchema);
module.exports = logins;
const usersSchema = new mongoose.Schema({
userName: {
type: String,
required: true,
},
email: {
type: String,
unique: true,
},
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'logins',
unique: true
}
})
What I would like to do is to create a new schema and reference the userSchema in it but not with the objectid. I would like to use the email property.
For Example
const usersSchema = new mongoose.Schema({
name1: {
type: String,
required: true,
},
name2: {
type: String,
unique: true,
},
emailref: {
/// reference the username schema with the email property instead of the objectId
}
})
How can I do that in mongoose?
I am trying to grab documents based on populated subdocuments.
Here are my models
// User model
var UserSchema = new mongoose.Schema({
username: {type: String, required: true, trim: true},
firstName: {type: String, required: true, lowercase: true},
lastName: {type: String, required: true, lowercase: true},
phone: {type: String, required: false},
email: {type: String, required: true},
password: {type: String, required: true},
blogs: {type: mongoose.Schema.Types.ObjectId, ref: 'Blogs'}
}, {timestamps: true});
// Blog Model
var BlogSchema = new mongoose.Schema({
description: String,
tags: [String],
other: [Object],
}, {timestamps: true});
This is how I am grabbing documents
fetchAllByFilter: async function(req, res) {
try {
let result = await Users.find({}).populate('blog');
return res.status(200).send(result);
} catch (err) {
return res.status(200).send({error: err});
}
},
Now my main question is, how would I grab Users based on their Blogs referenced documents?
For example, Find Users with Blogs that has Blog.tags of "food", "cars", "movies" and/or Blog.other of [{...SomeObject}, {...SomeOtherObject}]
looking at mongo docs match an array, you could make a utility function somewhat like this...
async function findByTag(tag) {
const blogIds = await Blog.find({ tags: tag }).select("_id");
const users = await User.find({
blogs: { $in: blogIds.map((blog) => blog._id) }
}).populate("blog");
}
I have a model for employee information im using in a MEAN stack and want to reference the store name they will work at in a stores model, would the following be correct?
Employees:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var muv = require('mongoose-unique-validator');
var schema = new Schema({
firstName: {type: String, required: true},
lastName: {type: String, required: true},
empId: {type: String, required: true, unique: true},
num: {type: String, required: true},
job: {type: String, required: true},
store: {type: Schema.Types.storeName, ref: 'Stores'},
});
schema.plugin(muv);
module.exports = mongoose.model('Message', schema);
Stores:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var schema = new Schema({
storeName: {type: String, required: true},
lat: {type: String, required: true},
long: {type: String, required: true},
});
module.exports = mongoose.model('Stores', schema);
You're close! You want to reference the ID of the store, not the name. When Mongoose does the 'Join', it will see the ObjectID store in the store profile field and match it to an ObjectID in the Stores collection.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var muv = require('mongoose-unique-validator');
var schema = new Schema({
firstName: {type: String, required: true},
lastName: {type: String, required: true},
empId: {type: String, required: true, unique: true},
num: {type: String, required: true},
job: {type: String, required: true},
store: {type: type: Schema.ObjectId, ref: 'Stores'}, //Reference store ObjectId.
});
schema.plugin(muv);
module.exports = mongoose.model('Message', schema);