how to update 2nd schema using node js (mongdb) - javascript

I am trying to update my 2nd schema which having reference in first schema
ownerSchema.js
var ownerSchema = Schema({
fname : String,
lname : String,
shopPlace : {
type: Schema.Types.ObjectId,
ref: 'Shop'
}
});
var Owner = mongoose.model('Owner', ownerSchema);
shopSchema.js
var shopSchema = Schema({
shopName : String,
location : String,
startDate : Date,
endDate : Date
});
var Shop = mongoose.model('Shop', shopSchema);
so I am trying to update my schema like this
const update = async (req, res) => {
const { id } = req.params;
let update = {};
if (req.body.fname) update.fname = req.body.fname;
if (req.body.lname) update.lname = req.body.lname;
if (req.body.shopPlace.shopName) update.shopPlace.shopName = req.body.shopPlace.shopName;
if (req.body.shopPlace.location) update.shopPlace.location = req.body.shopPlace.location;
let newOwmer = new Owner.updateOne(
{ ownerId: id },
{
$set: update,
},
{ runValidators: true }
);
};
I am trying to update shop but its not working and where am i wrong i dont know

const update = async (req, res) => {
const { id } = req.params;
let update = {};
let updateShop = {}
if (req.body.fname) update.fname = req.body.fname;
if (req.body.lname) update.lname = req.body.lname;
if (req.body.shopPlace.shopName) updateShop.shopPlace.shopName = req.body.shopPlace.shopName;
if (req.body.shopPlace.location) updateShop.shopPlace.location = req.body.shopPlace.location;
// get shopPlace _id from Owner
const { shopPlace } = await Owner.findOneAndUpdate(
{ _id: id },
{
$set: update,
},
{ new:true}
);
// here you must have the document you just updated. Just recover the id of the shop to modify it in turn
// update shop
let newShop = await Shop.findOneAndUpdate(
{ _id: shopPlace },
{
$set: updateShop,
},
{new:true}
);
};

Related

Why could the findOrCreate plugin create several documents at once?

I'm currently working on a MERN application with following/follower function for the users. I decided to create separate schemas for following and follower relationships detached from user schema.
Follower schema
const mongoose = require('mongoose');
const findOrCreate = require('mongoose-findorcreate');
const ObjectId = mongoose.Schema.Types.ObjectId;
const followerSchema = mongoose.Schema({
userId: {
type: ObjectId,
ref: 'User'
},
follower: {
type: [ObjectId],
ref: 'User'
}
});
followerSchema.plugin(findOrCreate);
const Follower = mongoose.model('Follower', followerSchema);
module.exports = { followerSchema, Follower };
Following schema
const mongoose = require('mongoose');
const findOrCreate = require('mongoose-findorcreate');
const ObjectId = mongoose.Schema.Types.ObjectId;
const followingSchema = mongoose.Schema({
userId: {
type: ObjectId,
ref: 'User'
},
following: {
type: [ObjectId],
ref: 'User'
}
});
followingSchema.plugin(findOrCreate);
const Following = mongoose.model('Following', followingSchema);
module.exports = { followingSchema, Following };
The problem however starts in my service where followings are created as supposed to. However, for followers mongoose create 6-8 documents at once with userIds that don't even exist in my db.
Here's the code of the followerService (it's the first function)
const { Follower } = require('../models/followerModel');
const { Following } = require('../models/followingModel');
const { User } = require('../models/userModel');
const mongoose = require('mongoose');
exports.changeFollowStatus = async (user, userId) => {
console.log({ userId: userId, user: user._id });
const newFollowing = await Following.findOrCreate({ userId: user._id }, (err, following, created) => {
console.log({following: following});
if (!err && !isFollowing(following, userId)) {
following.following.push(mongoose.Types.ObjectId(userId));
following.save();
User.findByIdAndUpdate(mongoose.Types.ObjectId(userId), {$inc: {follower: 1}});
} else {
const followingIndex = following.following.indexOf(mongoose.Types.ObjectId(userId));
following.following.splice(followingIndex, 1);
following.save();
User.findByIdAndUpdate(mongoose.Types.ObjectId(userId), { $inc: { follower: -1 } });
}
});
const newFollower = await Follower.findOrCreate({ userId: mongoose.Types.ObjectId(userId) }, (err, follower, created) => {
console.log({follower: follower});
if (!err && !isFollower(follower, user._id)) {
follower.follower.push(user._id);
follower.save();
User.findByIdAndUpdate(user._id, { $inc: { following: 1 } });
} else {
const followerIndex = follower.follower.indexOf(user._id);
follower.follower.splice(followerIndex, 1);
follower.save();
User.findByIdAndUpdate(user._id, { $inc: { following: -1 } });
}
});
};
exports.showFollowings = async (userId) => {
const followings = await Following.findOrCreate({ userId: mongoose.Types.ObjectId(userId) });
return followings.following;
};
exports.showFollowers = async (userId) => {
const followers = await Follower.findOrCreate({ userId: mongoose.Types.ObjectId(userId) });
return followers.follower;
};
const isFollowing = (newFollowing, userId) => {
return newFollowing.following.includes(mongoose.Types.ObjectId(userId));
};
const isFollower = (newFollower, userId) => {
return newFollower.follower.includes(userId);
}
Now, my following adding code and follower adding code look almost identical, but for some reason for followers, mongoose creates many more documents. The strange thing is that there is a follower document with the correct userId, but many other with random userIds get created which doesn't happen with followings which works as supposed to.
I also checked whether I pass the correct values and everything seems to be fine. But just for reference, here's the controller function from which I pass the values.
exports.changeFollowingStatus = async (req, res, next) => {
passport.authenticate('jwt', async (err, user, info) => {
if (err) {
console.error({ authError: err });
};
if (info !== undefined) {
console.error({ authError: info.message });
res.status(403).send(info.message);
} else {
console.log({params: req.params});
const userToFollow = req.params.id;
await FollowerService.changeFollowStatus(user, userToFollow);
res.status(200).send({ message: 'success' })
}
})(req, res, next);
};
Could anyone help me with this bug or at least navigate me towards the right direction? I can't seem to find solution to my problem. Thank you all in advance!

Mongoose update update nested object inside an array

I'm trying to query and update an element in the rosters array ( roster.schedule.monday.start) & then update the value in this example.
monday.start these two keys need to be dynamic
I think the approach would be something like this
Find document by _id
find matching object in array by _id
update nested values
I have tried this below with no luck, could anybody assist in this problem
many thanks
// Mongoose query
exports.updateRoster = (req, res) => {
const editDay = req.body.day;
const value = req.body.valueOfEntry;
const userId = req.body.id;
const rosterId = req.body.rosterId;
const startPerieod = req.body.time;
let dynObj = {
["rosters.$.schedule.$." + editDay + ".$." + startPerieod]: value,
};
Carer.updateOne({ "rosters._id": rosterId }, { $set: dynObj }).exec(
(err, roster) => {
if (err) {
return res.status(400).json({
error: err,
});
}
res.json(roster);
}
);
};
// Schema
const mongoose = require("mongoose");
const { ObjectId } = mongoose.Schema;
const carersSchema = new mongoose.Schema({
rosters: [
{
schedule: {
monday: {
start: { type: String },
finish: { type: String },
notes: { type: String },
},
],
});
module.exports = mongoose.model("Carers", carersSchema);
Try using $set and array filters like in the link
Carer.findOneAndUpdate({_id: carerId},
{
"$set": {[`rosters.$[outer].schedule.${editDay}.${startPerieod}`]: value}
},
{
"arrayFilters": [{ "outer._id": roasterId }]
},
function(err, response) {
if(err) console.log(err)
console.log(response)
})

How to delete related schema in mongodb using node js (mongoose)

I am trying to delete my 2nd schema which having reference in first schema
ownerSchema.js
var ownerSchema = Schema({
ownerId : String,
fname : String,
lname : String,
shopPlace : {
type: Schema.Types.ObjectId,
ref: 'Shop'
}
});
var Owner = mongoose.model('Owner', ownerSchema);
shopSchema.js
var shopSchema = Schema({
_id : String,
shopName : String,
location : String,
startDate : Date,
endDate : Date
});
var Shop = mongoose.model('Shop', shopSchema);
so I am trying to delete my 2nd schema but it only deleting 1st schema
const Owner = require("../models/ownerSchema");
const Shop = require("../models/shopSchema");
const deleteOnwerById = async (req, res) => {
const { id } = req.params;
let deletedShop = await Shop.deleteOne({_id:shopPlace}); // I can't delete my shop data
let deletedTask = await Owner.deleteOne({ ownerId: id });
} // I can delete owner but not shop detail
};
but it not deleting my related schema
First, delete your Owner using findByIdAndRemove or findOneAndDelete. These methods allow you to specify one or several fields to return from the deleted documents by passing it an optional options object.
let deletedOwner = await Owner.findByIdAndRemove(ownerId, {projection : "shopPlace"});
Then use the owner's shopPlace to delete the corresponding shopPlace :
await Shop.deleteOne({_id:deletedOwner.shopPlace});
You must first get an _id from shopPlace of your owner:
const Owner = require("../models/ownerSchema");
const Shop = require("../models/shopSchema");
const deleteOnwerById = async (req, res) => {
const { id } = req.params;
const owner = await Owner.findOne({ ownerId: id }).populate('shopPlace');
let deletedShop = await Shop.deleteOne({ _id:owner.shopPlace._id });
let deletedTask = await Owner.deleteOne({ ownerId: id });
}
};

MongoDB if field does not exist insert

i have category schema and i want to insert new category if category name does not exist. I tried something but i could not any response.
category.model
var mongoose = require("mongoose");
var categorySchema = mongoose.Schema({
name: {
type: String,
require: true
},
createdAt: {
type: Date,
default: () => {
return new Date();
}
}
});
module.exports = mongoose.model("category", categorySchema);
category insert function
var Category = require("../models/category.model");
exports.create = (req, res) => {
Category.find({ name: req.body.name }, (err, cat) => {
if (cat.length > 0) {
// i want to return exists message here
} else {
// i want to insert here if not exists
var category = new Category();
category.name = req.body.name;
category.save(err => {
if (err) {
return new response(null, err).error500(res);
}
return new response(category, null).created(res);
});
}
});
};
You can do it like this code below:
exports.create = async (req, res) => {
try {
let category = await Category.find({ name: req.body.name });
if(category) {
// return or do some stuff here
}
category = new Category(req.body);
category = await category.save();
return new response(category, null).created(res);
} catch(ex) {
console.log(ex.message);
return new response(null, ex).error500(res);
}
};
I hope it can help you.

Why is this mongoose 'findOne' query always returning null?

I am trying to find a specific document with mongoose in my Cosmosdb with this query described below.
const mongoose = require('mongoose');
var ObjectID = require('mongodb').ObjectID
const keys = require('../config/keys');
const Item = mongoose.model('items');
const uploadToBlob = async (containerName, blobName, json, id) => {
console.log('id', id)
Item.findOne({ _id: id }, (foundItem) => {
console.log(foundItem)
});
console.log('here')
Item.findOneAndDelete({ name: blobName });
};
I am successfully able to find the document when querying like this below.
const scanMongo = () => {
Item.find({
_id: {
$gt: ObjectID.createFromTime(Date.now() / keys.mongoPurgeInterval)
}}, (err, foundItems) => {
if(err) {
console.log("Oops", err);
return;
}
foundItems.forEach(item => {
JSON.stringify(item)
const blobName = item.name;
json = "'"+item+"'"
const id = item._id
uploadToBlob(keys.containerName, blobName, json, id);
});
});
}
This is what the object I'm looking for looks like when pulled from the query above.
[ { _id: 5cabd5c6e16288230cba2cf6, name: 'test', value: 1, __v: 0 } ]
For kicks, here my model.
const mongoose = require('mongoose');
const { Schema } = mongoose;
const itemSchema = new Schema({
name: String,
value: Number,
});
mongoose.model('items', itemSchema);
I'm befuddled. Any help would be bawler. Thanks!!!
Yeah, the first parameter is supposed to catch the error.
Item.findOne({ _id: id }, (error, foundItem) => {
console.log(foundItem)

Categories

Resources