How add children(ref) to mongoose? - javascript

My lines are empty.
My scheme:
// models/Tobacco.js
const mongoose = require('mongoose');
// FYI: important for populate work!!!
const tobaccoLineSchema = mongoose.Schema({
name: {type: String, required: true},
subTitle: {type: String, required: true},
count: {type: Number, required: true}
});
const Schema = mongoose.Schema;
const TobaccoSchema = new mongoose.Schema({
createdAt: {
type: Date,
default: Date.now
},
title: {
type: String,
required: true
},
subTitle: {
type: String,
required: false
},
bannerSrc: {
type: String,
required: false
},
socials: [],
lines: [
{
type: Schema.Types.ObjectId,
ref: 'tobaccoLine'
}
]
}
,{toObject:{virtuals:true}, toJSON:{virtuals:true}});
const TobaccoLine = mongoose.model('tobaccoLine', tobaccoLineSchema);
module.exports = mongoose.model('tobacco', TobaccoSchema);
Added populate line in API
router.get('/:id', (req, res) => {
Tobacco.findById(req.params.id).populate('lines')
.then(tobacco => res.json(tobacco))
.catch(err => res.status(404).json({ notobaccofound: 'No Tobacco found' }));
});
Database:
How I can't see issues. Client side fetches tobaccos but lines are empty.

Need to add back property into the child entity scheme.
const tobaccoLineSchema = mongoose.Schema({
name: {type: String, required: true},
subTitle: {type: String, required: true},
count: {type: Number, required: true},
_tobacco: { type: Schema.Types.ObjectId,
ref: 'tobacco' }
});

Related

How can I populate Ref Id to the orders?

How can I get and populate the id ref to the order? I grab the id of the product and user. Then when I use my get method, I will get all the details of the products and users.
For example, my data will show all information
Product
a. title
b. price
c. productImage
User
a. username
b. studentId
I tried to use populate, but I think I'm doing it wrong.
export const getOrders = async (req,res) =>{
try {
const order = await Order.findOne({productId: '634e91d256326381d5716993'})
.populate()
.exec()
res.status(400).json(order)
} catch (error) {
res.status(404).json({message: error.message})
}
}
OrderSchema
const OrderSchema = mongoose.Schema({
productId: {type: mongoose.Schema.Types.ObjectId, ref: 'Product', required: true},
buyerId: {type: mongoose.Schema.Types.ObjectId, required: true, ref: 'User'},
sellerId: {type: mongoose.Schema.Types.ObjectId, required: true, ref: 'User'}
})
export default mongoose.model('Order', OrderSchema)
ProductSchema
const ProductSchema = mongoose.Schema({
user_id: {type: mongoose.Schema.Types.ObjectId, required: true, ref: 'User'},
title: {type: String},
description: {type: String},
categories: {type: Array},
price: {type: Number},
productImage: {type: String}
},
{ timestamps: { createdAt: true } }
)
export default mongoose.model('Product', ProductSchema)
UserSchema
const UserSchema = mongoose.Schema({
username: {type: String, unique: true, required: true},
password: {type: String, required: true},
email: {type: String, unique: true, required: true},
studentid: {type: String, unique: true, required: true},
isAdmin:{type: Boolean, default: false},
},
{ timestamps: { createdAt: true } }
)
export default mongoose.model('User', UserSchema)
You have to specify what you want to populate inside the .populate() method:
await Order.findOne({productId: '634e91d256326381d5716993'})
.populate([
{ path: 'productId', select: 'title price productImage' },
{ path: 'buyerId', select: 'username studentid' }
])
.exec()

Mongoose adding values to an array

How can I add a User to the School's admins field?
School Schema:
const School = new Schema({
name: { type: String, required: true },
grades_primary: [{ type: Schema.Types.ObjectId, ref: 'Grade' }],
grades_secondary: [{ type: Schema.Types.ObjectId, ref: 'Grade' }],
admins: [{ type: Schema.Types.ObjectId, ref: 'User' }]
})
User Schema
const User = new Schema({
complete_name: { type: String, required: true, },
password: { type: String, required: true },
nombre_de_usuario: { type: String, unique: true, required: true },
email: { type: String, unique: true, required: true },
})
I tried this but it didn't work:
const saved_user = await user.save()
created_school.admins.push(saved_user)
await created_school.save()
use $push
const saved_user = await user.save()
try {
const created_school= await School.findOneAndUpdate({ _id: '1' }, { $push: { admins: saved_user } })
// ....
} catch (error) {
console.log(error);
}

What is the best way to attach properties to mongoose documents

I have an array of posts and each post contains an author id. I want to loop through each post and find author from User model by using author id and then attach it to post. What is the best and efficient way to do it. I am currently doing it this way, but it decreases the performance. Thanks.
posts = await Promise.all(
posts.map(async post => {
post.author = await User.findById(post.author).lean();
return post;
})
);
// POST SCHEMA
const postSchema = new mongoose.Schema({
author: {
type: String,
required: true
},
body: {
type: String,
required: true
},
post_image: {
url: String,
public_id: String,
width: Number,
height: Number
},
date_created: {
type: Date,
default: Date.now()
}
});
// USER SCHEMA
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
register_date: {
type: Date,
required: true,
default: Date.now()
},
friends: {
type: Array,
default: []
}
});
// NEW POST SCHEMA
const postSchema = new mongoose.Schema({
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users',
required: true
},
body: {
type: String,
required: true
},
post_image: {
url: String,
public_id: String,
width: Number,
height: Number
},
date_created: {
type: Date,
default: Date.now()
}
});
// USER SCHEMA
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
register_date: {
type: Date,
required: true,
default: Date.now()
},
friends: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'users',
required: true
}],
});
You can use auto population of mongo object in mongoose. It won't cause performance issues as it uses id index. Similar to this doc: https://mongoosejs.com/docs/populate.html
Your query will look like this:
const post = await Post.find({ author: { $in: req.user.friends }})
.populate('author')
.exec();
console.log(post);
Or you can use aggregate according to this document: https://mongoosejs.com/docs/api/aggregate.html
Your query will then look like:
const query = [
{ $match: { author: { $in: req.user.friends } } },
{ $lookup: { from: "users", localField: "author", foreignField: "_id", as: "authorDetails" } },
{ $unwind: "authorDetails" },
]
const post = await Post.aggregate(query).exec();
console.log(post);

I am getting E11000 duplicate key error collection: restauracja.tables index: reservations.user.email_1 dup key: { : null }

This is my Table Collection:
const tableSchema = new Schema({
number: {
type: Number,
required: true,
unique: true
},
seats: {
type: Number,
required: true
},
reservations: [orderSchema]
})
const Table = mongoose.model('Table', tableSchema)
And OrderSchema:
const orderSchema = new Schema({
time: {
type: String,
required: true
},
date: {
type: String
},
user: userSchema
})
And User Collection and Schema:
const userSchema = new Schema({
firstname: {
type: String,
required: true
},
lastname: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
phone: {
type: String
},
password: {
type: String,
required: true
},
admin: {
type: Boolean,
default: false,
required: true
}
})
const User = mongoose.model('User', userSchema)
When i would like to create the second (i have no idea why first is adding) instance of table, i am getting error like in title:
MongoError: E11000 duplicate key error collection: restauracja.tables index: reservations.user.email_1 dup key: { : null }
I can create only one document in table, but i have different values in the second one.

MongoDB Chat Schema

Im trying MongoDB and as a matter of starting point im creating a schema for a chat application that may be simple to scale in the future, so im wondering if this looks correct from what i have been seeing in the docs. So far i have 3 collections User, Room, Message. Also i would need to perform some queries like getting all messages from a sepecific room, get all messages from a specific user, etc
Designed with mongoose:
var user = new mongoose.Schema({
username: { type: String, lowercase: true, unique: true },
email: { type: String, lowercase: true, unique: true },
password: String,
is_active: { type: Boolean, default: false },
});
var room = new mongoose.Schema({
name: { type: String, lowercase: true, unique: true },
topic: String,
users: [user],
messages: [message],
created_at: Date,
updated_at: { type: Date, default: Date.now },
});
var message = new mongoose.Schema({
room: room,
user: user,
message_line: String,
created_at: { type: Date, default: Date.now },
});
var User = mongoose.model('User', user);
var Room = mongoose.model('Room', room);
var Message = mongoose.model('Message', message);
//message model
'use strict';
import mongoose from 'mongoose';
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;
var MessageSchema = new Schema({
send: {
type: ObjectId,
ref: 'User',
required: true
},
message: {
type: String,
required: true
},
date: {
type: Date
},
created_by: {
type: ObjectId,
ref: 'User',
required: true
},
thread: {
type: ObjectId,
ref: 'MsgThread',
required: true
},
is_deleted: [{
type: ObjectId,
ref: 'User'
}]
}, {
timestamps: {
createdAt: 'created_at',
updatedAt: 'last_updated_at'
}
});
export default mongoose.model('Message', MessageSchema);
//dont use rooms,,use thread like groupchat or personalChat
import mongoose from 'mongoose';
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;
const s3 = require("../../utils/s3");
var MsgThreadSchema = new Schema({
users: [{
type: ObjectId,
ref: 'User'
}],
group_name: {
type: String
},
created_by: {
type: ObjectId,
ref: 'User'
},
community: {
type: ObjectId,
ref: 'Community'
},
image_url: {
type: String
}
}, {
timestamps: {
createdAt: 'created_at',
updatedAt: 'last_updated_at'
}
});
export default mongoose.model('MsgThread', MsgThreadSchema);

Categories

Resources