How to create array of object ids in ref mongoose? - node.js

I am trying to create an order in the post method. I have two documents - Order, OrderItem. Schemas are-
var OrderSchema = new Schema({
name: String,
orderItems : [{ type: Schema.Types.ObjectId, ref: 'orderitems' }]
});
var orderItemsSchema = new Schema({
name: String,
products : String
});
My controller function --
let itemArr: any[] = [];
req.body.orderItems.map(async (item: { products: any; quantity: any }) => {
const newOrdeItem = new OrderItems({
products: item.products,
quantity: item.quantity,
});
const items = await newOrdeItem.save();
//found ids
itemArr.push(items._id);
});
//not found ids
itemArr.push(items._id);
const newOrder = new Order({
orderItems: itemArr,
phone: req.body.phone,
});
const order = await newOrder.save();
return res.json(order);
I want to make an order. req.body data are--
{
"orderItems" : [
{
"quantity": 3,
"product" : "Orange"
},
{
"quantity": 2,
"product" : "Banana"
}
],
"phone": "+420702241333",
}
How can I solve this issue?

I understand you need to get the orderItems from the ref ? if that can you use populate
order.populate('orderItems')
//Edit
frist require your model from monngoose
after that make insertMany or save fn
like
const dbOrders = require('pathOfModel')
orderResult = await dbOrders.insertMany({
name : req.body.name || 'Name you need insert in Schema',
orderItems : itemArr
})
console.log(orderResult) // res.json(orderResult)

Related

Ref in mongoose model not giving output

I am using mongoose for defining schema. I have two schemas user and Userdetail. i want data from user in userdetail
I have below schema but i am not getting the output. i think the code is correct but not getting why there is no output...instead i am getting empty array.
const mongoose = require("mongoose")
const UserDetailSchema = mongoose.Schema({
Phone : {
type : Number
},
FirstName : {
type : String
},
LastName : {
type : String
},
productimage : {
data : Buffer,
contentType : String
},
IsDeleted:{
type:Boolean,
default:false
},
UserID : {
type : String,
},
data : [{
type: mongoose.Schema.Types.ObjectId,
ref: "user"
}],
},
{timestamps: true})
const UserDetail = new mongoose.model("userdetail",UserDetailSchema);
module.exports = UserDetail;
my user schema is,
const mongoose = require("mongoose");
const UserSchema = mongoose.Schema({
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
IsDeleted:{
type:Boolean
},
},
{timestamps: true});
module.exports = mongoose.model("user", UserSchema);
query is,
<pre>
router.get("/UserDetail",async (req,res)=>{
try{
const UsersData= await UserDetail.find();
res.json(UsersData)
}catch(e){
res.status(500).json({ message: e.message })
}
})
</pre>
Even though i am using only find, i must get the data with only id right?
Output is -
Any help would be appreciated
router.patch("/UserDetail/:id",Auth,upload.single("productimage"),async(req,res)=>{
try{
const id = req.params.id;
const updatedData = req.body;
updatedData.productimage = {data: fs.readFileSync('upload/' + req.file.filename),
contentType: 'image/png'};
const options = { new: true };
const result = await UserDetail.findOneAndUpdate(
id, updatedData, options
)
res.send(result)
}catch(e){
res.status(500).json({ message: e.message })
}
})
You can populate a field with the populate function:
const userDetails = await UserDetail.find({}).populate('data').exec();
firstly you need a little change in userID in schema of userDetail.Please make it to UserID:{type : mongoose.Schema.Types.ObjectId}, as it will help you in future during aggregation and you can also remove data from your userDetail model as it will not store any data until you save it.And lastly try to run this aggregation query.
const UsersData= await UserDetails.aggregate([
{$lookup:
{
from: "users",
localField: "userID",
foreignField: "_id",
as: "data"
}
}])
In this way your respective details of users will be displayed in array of data.
Make changes in your model and then populate the data.
const mongoose = require("mongoose")
const UserDetailSchema = mongoose.Schema({
Phone : {
type : Number
},
FirstName : {
type : String
},
LastName : {
type : String
},
productimage : {
data : Buffer,
contentType : String
},
IsDeleted:{
type:Boolean,
default:false
},
UserID : {
type : String,
},
data : {
type: mongoose.Schema.Types.ObjectId,
ref: "user"
},
},
{timestamps: true})
}
populate query
let Model=//import your model here
let userdata=await Model.find().populate("data")
console.log(userdata)

mongoose save internal documents creating a document that contains a document

Im learning mongoose and i have a question how to save several documents:
// Product.js
const categorySchema = mongoose.Schema(
{ name: String },
{ collection: "categories" }
);
const productSchema = mongoose.Schema(
{ name: String, category: categorySchema },
{ collection: "products" }
);
modules.exports = mongoose.model("Product", productSchema);
The idea is that when I create a product this way
const Product = require("./Product.js")
const product = new Product({name: 'Soccer Ball', category: {name: "Sports"})
await product.save()
i want to get a document in the collection products and also a document in the collection categories
how can it be possible
thanks in advance
PD : Im getting this but category is not save in the collection
{
"msg": "Product created succesfully",
"ok": true,
"product": {
"name": "Soccer ball",
"category": {
"name": "Sports",
"_id": "6275df4c8149967bea21e7c0"
},
"_id": "6275df4c8149967bea21e7bf",
"__v": 0
}
}
You should define your Product's category as a ref attribute:
// Product.js
const categorySchema = mongoose.Schema(
{ name: String },
{ collection: 'categories' }
);
const productSchema = mongoose.Schema(
{
name: String,
category: { type: mongoose.Schema.Types.ObjectId, ref: 'categories' },
},
{ collection: 'products' }
);
modules.exports = {
Category: mongoose.model('Category', categorySchema),
Product: mongoose.model('Product', productSchema),
}
Doing this you will need to assign the _id of the category to the new Product:
const { Category } = require("./Product.js")
const { Product } = require("./Product.js")
// Create category (supposing it is not present)
const category = new Category({ name: "Sports" })
await category.save()
// Add category _id to product
const product = new Product({name: 'Soccer Ball', category: category._id})
await product.save()
Finally, you will be able to retrieve the product by using populate:
const product = await Product.findById(<product_id>).populate('categories').exec()
This query should give the same result than before, but the Category data will be loaded from the reference in the Category collection.

Mongoose - Find document's child array by a specific field

I had spent hours trying to work out how to get records from a document's child array by a specific field, but I failed it.
I would like to pass a personId by a web service to find which meeting he/she has been invited to. As a result, I could track down whether the invitee has accept to join the meeting or not.
Basically, I have the following JSON output:
{
"status": "success",
"requestedAt": "2021-03-28T22:47:03+11:00",
"size": 1,
"meetings": [
{
"invitedMembers": [
{
"isJoined": false,
"_id": "605ffbc00a21ed718c992549",
"person": "a123",
"__v": 0
}
]
}
]
}
with a controller like this:
const memberId = "a123";
const meetings = await Meeting.find({
'invitedMembers.member': memberId
}).populate('invitedMembers');
a meeting model class like below:
const mongoose = require('mongoose');
const meetingSchema = new mongoose.Schema({
invitedMembers: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'InvitedMembers'
}
]
});
const Meeting = mongoose.model(
'Meeting',
meetingSchema
);
module.exports = Meeting;
and a invitedMembers class like this:
const mongoose = require('mongoose');
const invitedMembersSchmea = new mongoose.Schema({
member: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Member',
required: true
},
isJoined: {
type: Boolean,
default: false
}
});
const InvitedMembers = mongoose.model(
'InvitedMembers',
invitedMembersSchmea
);
module.exports = InvitedMembers;
The Member schema only contains a basic personal information such as first name, last name and etc.
I ended up solving my own problem by using a different approach where I changed my data structure by adding invitedMembers as an embedding model in the meeting model and updated the person field in the invitedMembers schema to _id.
Updated Meeting model class:
const mongoose = require('mongoose');
const invitedMembersSchmea = new mongoose.Schema({
_id: {
type: String,
required: true
},
isJoined: {
type: Boolean,
default: false
}
});
const meetingSchema = new mongoose.Schema({
invitedMembers: [
{
type: invitedMembersSchmea
}
]
});
const Meeting = mongoose.model(
'Meeting',
meetingSchema
);
module.exports = Meeting;
As a result, I can find the invited member by ID using the following query:
const memberId = "a123";
const meetings = await Meeting.find({
'invitedMembers._id': memberId
});

How do we create a reference to a collection with mongoose

Please forgive me if this question seems related to the one I already asked before, I feel I didn't really put the question well the first time.
I have created a resource route based on the following data seeded into Mongodb from my application
// seeder.js
"_id": "5d7a514b5d2c12c7449be020",
"issuedBy": "Ola",
"collectedBy": "Ola",
"quantity": "8",
"product": "5d713995b721c3bb38c1f5d0",
My question is this; How do i actually save the product objectId "5d713995b721c3bb38c1f5d0" on the Orders collection
exports.getOrders = asyncHandler(async (req, res, next) => {
if (req.params.productId) {
const orders = await Orders.find({ product: req.params.productId });
return res.status(200).json({
success: true,
count: orders.length,
data: orders
});
} else {
res.json(orders);
}
});
With the above route, I am setting a condition that's based on the presence of a ProductId in the URL that will be matched inside the Orders collection "Orders.find({ product: req.params.productId })"
//Product Schema
const ProductSchema = new mongoose.Schema({
name : String,
description : String,
price : Number,
quantity : Number,
supplier :String
},{timestamps:true});
module.exports = mongoose.model('Product', ProductSchema)
// Orders Schema
const OrderSchema = new mongoose.Schema({
issuedBy : String,
collectedBy: String,
quantity: Number,
product: {
type: mongoose.Schema.ObjectId,
ref: 'Product',
required: true
},
},{timestamps:true});
const Orders = mongoose.model("Orders", OrderSchema);
// Export model
module.exports = Orders;
How do I actually create a route that saves the productId in the Orders collection like these "product": "5d713995b721c3bb38c1f5d0"?
I think an order can have multiple products. So I changed the name of product to producs, and made it array.
Also, for model names it is better to use singular naming convention.
I would set up my schemas and models like this:
Product model:
const mongoose = require("mongoose");
const ProductSchema = new mongoose.Schema(
{
name: String,
description: String,
price: Number,
quantity: Number,
supplier: String
},
{ timestamps: true }
);
module.exports = mongoose.model("Product", ProductSchema);
Order model:
const mongoose = require("mongoose");
const OrderSchema = new mongoose.Schema(
{
issuedBy: String,
collectedBy: String,
quantity: Number,
products: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Product",
required: true
}
]
},
{ timestamps: true }
);
module.exports = mongoose.model("Order", OrderSchema);
You can create an order with products with this code:
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const url = "mongodb://localhost:27017/ord";
const Order = require("./models/order");
const Product = require("./models/product");
const port = 3000;
app.use(express.json());
mongoose
.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
app.listen(port, () => {
console.log(`App running on port ${port}...`);
});
})
.catch(error => console.log(error));
app.post("/order", async (req, res) => {
let result = await Order.create(req.body);
res.send(result);
});
app.get("/order/:orderId", async (req, res) => {
const result = await Order.findById(req.params.orderId).populate("products");
res.send(result);
});
You can create an order with products to the http://localhost:3000/order with this body: (you must use your existing product ids)
{
"issuedBy": "issuedBy",
"collectedBy": "collectedBy",
"quantity": 123,
"products": ["5ddfb388b14c5b41e0607a5e","5ddfb376b14c5b41e0607a5d"]
}
Response:
{
"products": [
"5ddfb388b14c5b41e0607a5e",
"5ddfb376b14c5b41e0607a5d"
],
"_id": "5ddfb418b14c5b41e0607a5f",
"issuedBy": "issuedBy",
"collectedBy": "collectedBy",
"quantity": 123,
"createdAt": "2019-11-28T11:48:40.500Z",
"updatedAt": "2019-11-28T11:48:40.500Z",
"__v": 0
}
When you want to get this order and its products, you need to send a GET request to the http://localhost:3000/order/5ddfb418b14c5b41e0607a5f the id in the url is the the id of the order we previosly created, so you need to use your order id.
If you want also to be able to add a product to an existing order, you can add this code:
app.post("/order/:orderId/:productId", async (req, res) => {
const result = await Order.findByIdAndUpdate(
req.params.orderId,
{
$push: {
products: req.params.productId
}
},
{ new: true }
);
res.send(result);
});
So the POST url must contain the orderId and productId like this:
http://localhost:3000/order/5ddfb418b14c5b41e0607a5f/5ddfb67c721b885790ec837b
Response:
{
"products": [
"5ddfb388b14c5b41e0607a5e",
"5ddfb376b14c5b41e0607a5d",
"5ddfb67c721b885790ec837b"
],
"_id": "5ddfb418b14c5b41e0607a5f",
"issuedBy": "issuedBy",
"collectedBy": "collectedBy",
"quantity": 123,
"createdAt": "2019-11-28T11:48:40.500Z",
"updatedAt": "2019-11-28T11:59:51.659Z",
"__v": 0
}

How can i send a query to Push details to my MongoDB using Node and mongoose [duplicate]

Basically I have a mongodb collection called 'people'
whose schema is as follows:
people: {
name: String,
friends: [{firstName: String, lastName: String}]
}
Now, I have a very basic express application that connects to the database and successfully creates 'people' with an empty friends array.
In a secondary place in the application, a form is in place to add friends. The form takes in firstName and lastName and then POSTs with the name field also for reference to the proper people object.
What I'm having a hard time doing is creating a new friend object and then "pushing" it into the friends array.
I know that when I do this via the mongo console I use the update function with $push as my second argument after the lookup criteria, but I can't seem to find the appropriate way to get mongoose to do this.
db.people.update({name: "John"}, {$push: {friends: {firstName: "Harry", lastName: "Potter"}}});
Assuming, var friend = { firstName: 'Harry', lastName: 'Potter' };
There are two options you have:
Update the model in-memory, and save (plain javascript array.push):
person.friends.push(friend);
person.save(done);
or
PersonModel.update(
{ _id: person._id },
{ $push: { friends: friend } },
done
);
I always try and go for the first option when possible, because it'll respect more of the benefits that mongoose gives you (hooks, validation, etc.).
However, if you are doing lots of concurrent writes, you will hit race conditions where you'll end up with nasty version errors to stop you from replacing the entire model each time and losing the previous friend you added. So only go to the latter when it's absolutely necessary.
The $push operator appends a specified value to an array.
{ $push: { <field1>: <value1>, ... } }
$push adds the array field with the value as its element.
Above answer fulfils all the requirements, but I got it working by doing the following
var objFriends = { fname:"fname",lname:"lname",surname:"surname" };
People.findOneAndUpdate(
{ _id: req.body.id },
{ $push: { friends: objFriends } },
function (error, success) {
if (error) {
console.log(error);
} else {
console.log(success);
}
});
)
Another way to push items into array using Mongoose is- $addToSet, if you want only unique items to be pushed into array. $push operator simply adds the object to array whether or not the object is already present, while $addToSet does that only if the object is not present in the array so as not to incorporate duplicacy.
PersonModel.update(
{ _id: person._id },
{ $addToSet: { friends: friend } }
);
This will look for the object you are adding to array. If found, does nothing. If not, adds it to the array.
References:
$addToSet
MongooseArray.prototype.addToSet()
Use $push to update document and insert new value inside an array.
find:
db.getCollection('noti').find({})
result for find:
{
"_id" : ObjectId("5bc061f05a4c0511a9252e88"),
"count" : 1.0,
"color" : "green",
"icon" : "circle",
"graph" : [
{
"date" : ISODate("2018-10-24T08:55:13.331Z"),
"count" : 2.0
}
],
"name" : "online visitor",
"read" : false,
"date" : ISODate("2018-10-12T08:57:20.853Z"),
"__v" : 0.0
}
update:
db.getCollection('noti').findOneAndUpdate(
{ _id: ObjectId("5bc061f05a4c0511a9252e88") },
{ $push: {
graph: {
"date" : ISODate("2018-10-24T08:55:13.331Z"),
"count" : 3.0
}
}
})
result for update:
{
"_id" : ObjectId("5bc061f05a4c0511a9252e88"),
"count" : 1.0,
"color" : "green",
"icon" : "circle",
"graph" : [
{
"date" : ISODate("2018-10-24T08:55:13.331Z"),
"count" : 2.0
},
{
"date" : ISODate("2018-10-24T08:55:13.331Z"),
"count" : 3.0
}
],
"name" : "online visitor",
"read" : false,
"date" : ISODate("2018-10-12T08:57:20.853Z"),
"__v" : 0.0
}
First I tried this code
const peopleSchema = new mongoose.Schema({
name: String,
friends: [
{
firstName: String,
lastName: String,
},
],
});
const People = mongoose.model("person", peopleSchema);
const first = new Note({
name: "Yash Salvi",
notes: [
{
firstName: "Johnny",
lastName: "Johnson",
},
],
});
first.save();
const friendNew = {
firstName: "Alice",
lastName: "Parker",
};
People.findOneAndUpdate(
{ name: "Yash Salvi" },
{ $push: { friends: friendNew } },
function (error, success) {
if (error) {
console.log(error);
} else {
console.log(success);
}
}
);
But I noticed that only first friend (i.e. Johhny Johnson) gets saved and the objective to push array element in existing array of "friends" doesn't seem to work as when I run the code , in database in only shows "First friend" and "friends" array has only one element !
So the simple solution is written below
const peopleSchema = new mongoose.Schema({
name: String,
friends: [
{
firstName: String,
lastName: String,
},
],
});
const People = mongoose.model("person", peopleSchema);
const first = new Note({
name: "Yash Salvi",
notes: [
{
firstName: "Johnny",
lastName: "Johnson",
},
],
});
first.save();
const friendNew = {
firstName: "Alice",
lastName: "Parker",
};
People.findOneAndUpdate(
{ name: "Yash Salvi" },
{ $push: { friends: friendNew } },
{ upsert: true }
);
Adding "{ upsert: true }" solved problem in my case and once code is saved and I run it , I see that "friends" array now has 2 elements !
The upsert = true option creates the object if it doesn't exist. default is set to false.
if it doesn't work use below snippet
People.findOneAndUpdate(
{ name: "Yash Salvi" },
{ $push: { friends: friendNew } },
).exec();
An easy way to do that is to use the following:
var John = people.findOne({name: "John"});
John.friends.push({firstName: "Harry", lastName: "Potter"});
John.save();
In my case, I did this
const eventId = event.id;
User.findByIdAndUpdate(id, { $push: { createdEvents: eventId } }).exec();
Push to nested field - use a dot notation
For anyone wondering how to push to a nested field when you have for example this Schema.
const UserModel = new mongoose.schema({
friends: {
bestFriends: [{ firstName: String, lastName: String }],
otherFriends: [{ firstName: String, lastName: String }]
}
});
You just use a dot notation, like this:
const updatedUser = await UserModel.update({_id: args._id}, {
$push: {
"friends.bestFriends": {firstName: "Ima", lastName: "Weiner"}
}
});
This is how you could push an item - official docs
const schema = Schema({ nums: [Number] });
const Model = mongoose.model('Test', schema);
const doc = await Model.create({ nums: [3, 4] });
doc.nums.push(5); // Add 5 to the end of the array
await doc.save();
// You can also pass an object with `$each` as the
// first parameter to use MongoDB's `$position`
doc.nums.push({
$each: [1, 2],
$position: 0
});
doc.nums;
// This is the my solution for this question.
// I want to add new object in worKingHours(array of objects) -->
workingHours: [
{
workingDate: Date,
entryTime: Date,
exitTime: Date,
},
],
// employeeRoutes.js
const express = require("express");
const router = express.Router();
const EmployeeController = require("../controllers/employeeController");
router
.route("/:id")
.put(EmployeeController.updateWorkingDay)
// employeeModel.js
const mongoose = require("mongoose");
const validator = require("validator");
const employeeSchema = new mongoose.Schema(
{
name: {
type: String,
required: [true, "Please enter your name"],
},
address: {
type: String,
required: [true, "Please enter your name"],
},
email: {
type: String,
unique: true,
lowercase: true,
required: [true, "Please enter your name"],
validate: [validator.isEmail, "Please provide a valid email"],
},
phone: {
type: String,
required: [true, "Please enter your name"],
},
joiningDate: {
type: Date,
required: [true, "Please Enter your joining date"],
},
workingHours: [
{
workingDate: Date,
entryTime: Date,
exitTime: Date,
},
],
},
{
toJSON: { virtuals: true },
toObject: { virtuals: true },
}
);
const Employee = mongoose.model("Employee", employeeSchema);
module.exports = Employee;
// employeeContoller.js
/////////////////////////// SOLUTION IS BELOW ///////////////////////////////
// This is for adding another day, entry and exit time
exports.updateWorkingDay = async (req, res) => {
const doc = await Employee.findByIdAndUpdate(req.params.id, {
$push: {
workingHours: req.body,
},
});
res.status(200).json({
status: "true",
data: { doc },
});
};
https://www.youtube.com/watch?v=gtUPPO8Re98
I ran into this issue as well. My fix was to create a child schema. See below for an example for your models.
---- Person model
const mongoose = require('mongoose');
const SingleFriend = require('./SingleFriend');
const Schema = mongoose.Schema;
const productSchema = new Schema({
friends : [SingleFriend.schema]
});
module.exports = mongoose.model('Person', personSchema);
***Important: SingleFriend.schema -> make sure to use lowercase for schema
--- Child schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const SingleFriendSchema = new Schema({
Name: String
});
module.exports = mongoose.model('SingleFriend', SingleFriendSchema);

Resources