How to use updateOne in Mongodb using NodeJS - javascript

Below is my code to update a website field in Mongodb using NodeJS, instead of updating however it adds a new field to my database.
let { ObjectId } = require('mongodb');
async get(id) {
if (!id) throw 'You must provide an id to search for';
const restaurantsCollection = await restaurants();
const res = await restaurantsCollection.findOne({ _id: id });
if (res === null) throw 'No dog with that id';
res._id = res._id.toString().replace(/ObjectId\("(.*)"\)/, "$1")
return res;
}
async rename(id, newWebsite) {
if (!id) throw 'You must provide an id to search for';
if (!newWebsite) throw 'You must provide a name for the restaurant';
let newid = ObjectId(id);
const restaurantsCollection = await restaurants();
const updatedInfo = await restaurantsCollection.updateOne(
{ _id: newid },
{ $set: {website:newWebsite} }
);
if (updatedInfo.modifiedCount === 0) {
throw 'could not update restaurant successfully';
}
return await this.get(newid);
}
};
Below is the output I get:
{
_id: new ObjectId("6157825a4dcae5beb8f759bf"),
name: 'Hey',
location: 'New York City, New York',
phoneNumber: '123-456-7890',
website: 'http://www.hey.com',
priceRange: '$$$$',
cuisines: [ 'Cuban', 'Italian' ],
overallRating: 3,
serviceOptions: { dineIn: true, takeOut: true, delivery: false },
newWebsite: 'http://www.hi.com'
}
Instead of the website being replaced from hey.com to hi.com it just adds a new field, what am I doing wrong?

Instead of using { $set: {website:newWebsite} }
It should just be {website:newWebsite}

Related

mongodb edit array don't update it

I'm trying to edit the collection of users where their purchase date has expired but when I'm saving, user.role have been updated but purchaseHistory don't have been updated.
Here is the function
async function checkExpiredPurchase() {
const users = await User.find({
"purchaseHistory.expdate": { $exists: true, $lte: new Date() }
});
for(const user of users) {
let purchases = await user.purchaseHistory.map(purchase => {
if(purchase.expdate && purchase.expdate <= new Date()) {
purchase.expdate = null;
}
return purchase
});
user.role = "Utilisateur"
user.purchaseHistory = purchases
console.log(user.purchaseHistory) //here it return [{"date":"2022-12-12T20:53:48.467Z","item":"VIP","expdate":null,"methode":"PayPal"}] which is what I want
user.save(function(err, resolve) {
if (err)
console.log('db error', err)
// saved!
});
}
}
And when I check on MongoDB Compass (after refreshing ofc), I see that the role have been updated but not the array with purchaseHistory since expdate is still egal to a date and not to "null"
And I've also tried with this code for update
User.updateOne(
{ _id: user.id },
{ $set: { purchaseHistory: purchases }, $set: { role: "Utilisateur" } },
).then(() => { console.log('should be good')})
But as well as the other one, role update, not purchaseHistory
There was never an error in the console. Thank you in advance for any answers you can bring me
I was not able to reproduce the problem, it worked here just fine:
var mongoose = require('mongoose');
mongoose.set('strictQuery', true);
(async () => {
await mongoose.connect("mongodb://user:password#0.0.0.0:50000", { dbName: "somedb" });
const User = mongoose.model("user-test", {
purchaseHistory: [{ expdate: Date, item: String }],
role: String
});
await User.deleteMany({});
await User.insertMany([
{ role: "User", purchaseHistory: [{ expdate: "1800-01-01", item: "VIP" }], },
{ role: "User 2", purchaseHistory: [{ expdate: "2050-01-01", item: "VIP" }], }
]);
const users = await User.find({
"purchaseHistory.expdate": { $exists: true, $lte: new Date() }
});
console.log("\r\nDOCUMENT BEFORE SAVE:\r\n\r\n", users[0]);
for (const user of users) {
user.purchaseHistory.forEach(purchase => {
if (purchase.expdate && purchase.expdate <= new Date()) {
purchase.expdate = null;
}
});
user.role = "Utilisateur"
console.log("\r\nDOCUMENT AFTER SAVE:\r\n\r\n", await user.save());
}
})();

Content/map is undefined

Let's say I have this:
const db = require('../../Models/warns')
const { Message, MessageEmbed } = require('discord.js')
const reason = args.slice(1).join(" ")
db.findOne({ guildid: message.guild.id, user: user.user.id}, async(err, data) => {
if(err) throw err;
if(!data) {
data = new db({
guildid: message.guild.id,
user : user.user.id,
content : [
{
moderator : message.author.id,
reason : reason
}
]
})
} else {
const obj = {
moderator: message.author.id,
reason : reason
}
data.content.push(obj)
}
data.save()
});
Why is push undefined (TypeError: Cannot read properties of undefined (reading 'push'))? Is there anything I am missing?
Same thing goes if I have this:
const sembed = new MessageEmbed()
.setTitle(`${user.user.tag}'s warns`)
.setDescription(
data.content.map(
(w, i) =>
`\`${i + 1}\` | Moderator : ${message.guild.members.cache.get(w.moderator).user.tag}\nReason : ${w.reason}`
)
)
.setColor("BLUE")
message.channel.send({ embeds: [sembed] });
map is also undefined.
(I am using discordjs v13)
Models/warns:
const { Schema, model } = require('mongoose');
module.exports = model("warns", new Schema({
userId: String,
guildId: String,
moderatorId: String,
reason: String,
timestamp: Number,
})
);
NOTE: This code is by using mongoose npm package (A database, pretty much like quick.db but better)

findOneAndUpdate is not a function of mongoose

I know this question has been answered before but I can't seem to implement the changes into what im working with. I'm trying to create a daily command that rewards a user for doing s!daily. I get the error,
TypeError: profileData.findOneAndUpdate is not a function
at Object.execute (C:\Users--\Desktop\DiscBot\commands\daily.js:35:43)
at module.exports (C:\Users--\Desktop\DiscBot\events\client\message.js:34:13)
daily.js, one having error at line 35 for findOneAndUpdate is not a function
const Schema = require('../models/profileSchema')
//cache users that claim daily rewards
let claimedCache = []
const clearCache = () => {
claimedCache = []
setTimeout(clearCache, 1000 * 60 * 10)
}
clearCache()
//message to make it easier later
const alreadyClaimed = 'You have already claimed your daily rewards'
module.exports = {
name: "daily",
aliases: ["day", "d"],
permissions: [],
description: "Claim your daily rewards!",
async execute(message, args, cmd, client, Discord, profileData) {
const { serverID, member } = message
const { id } = member
//If user is in cache return message
if (claimedCache.includes(id)) {
console.log('Returning from cache')
message.reply(alreadyClaimed)
return
}
//Put everything in object for later
const obj = {
guildId: serverID,
userId: id,
}
//Results is an update that either updates if is user is not in array and doesn't if they are, but it doesn't know what findOneAndUpdate is (thought it was just a mongo/mongoose function??)
try {
const results = await profileData.findOneAndUpdate(obj)
console.log('RESULTS:', results)
if (results) {
const then = new Date(results.updatedAt).getTime()
const now = new Date().getTime()
const diffTime = Math.abs(now - then)
const diffDays = Math.round(diffTime / (1000 * 60 * 60 * 24))
if (diffDays <= 1) {
claimedCache.push(id)
message.reply(alreadyClaimed)
return
}
}
//after the update increase coins by 50 and send claimed message
await profileRewardsSchema.findOneAndUpdate(obj, obj, {
upsert: true,
})
claimedCache.push(id)
const amount = 50;
await profileModel.findOneAndUpdate(
{
userID: id,
},
{
$inc: {
coins: amount,
},
}
);
message.reply('You have claimed your daily rewards!')
}catch (err) {
console.log(err);
}
}
}
message.js, heres where I make profileModel a thing using mongoose to pass it into my commands
const profileModel = require("../../models/profileSchema");
const config = require('../../config.json');
module.exports = async (Discord, client, message) => {
//command handler start
const prefix = 's!';
if (!message.content.startsWith(prefix) || message.author.bot) return;
//database junk
let profileData;
try {
profileData = await profileModel.findOne({ userID: message.author.id });
if (!profileData) {
let profile = await profileModel.create({
userID: message.author.id,
serverID: message.guild.id,
coins: 10,
bank: 0,
});
profile.save();
}
} catch (err) {
console.log("Error creating new database profile");
}
const args = message.content.slice(prefix.length).split(/ +/);
const cmd = args.shift().toLowerCase();
const command = client.commands.get(cmd) || client.commands.find(a => a.aliases && a.aliases.includes(cmd));
if(!command) return message.channel.send(":x: This is not a valid command");
try {
command.execute(message, args, cmd, client, Discord, profileData);
} catch (err) {
message.reply('There was an error executing that command!');
}
};
profileSchema.js, Where profile is made into mongo database
const mongoose = require("mongoose");
const profileSchema = new mongoose.Schema({
userID: { type: String, require: true, unique: true },
serverID: { type: String, require: true },
coins: { type: Number, default: 10 },
bank: { type: Number },
},
{
timestamps: true,
}
)
const model = mongoose.model("ProfileModels", profileSchema);
module.exports = model;
main.js, where mongoose is connected, then passed on
mongoose.connect(process.env.MONGODB_SRV, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
})
You are trying to call findOneAndUpdate on the document, which you passed to execute function at message.js. Check the example of how to use findOneAndUpdate
https://mongoosejs.com/docs/tutorials/findoneandupdate.html
Most of the time this error happen when you call findOneAndUpdate in mongoose when you call it on the instance of the model NOT the actual model
so instead of this
var NewUser = new User(req.user);
NewUser.findOneAndUpdate...
do this
var NewUser = new User(req.user);
User.findOneAndUpdate(
{ name: NewUser.name },
{ name: NewUser.name},
{ upsert: true });

Search an array of objects to match objectId from mongodb

I'm trying to search an array of objects by a key that contains nested object id for populating.
My object
{
service: 'user',
isModerator: true,
isAdmin: true,
carts: [
{
_id: 5e1344dcd4c94a1554ae0191,
qty: 1,
product: 5e09e4e0fcda6f268cefef3f,
user: 5e0dda6d6853702038da60f0,
expireAt: 2020-01-06T14:31:56.708Z,
__v: 0
},
{
_id: 5e13455306a54b31fc71b371,
qty: 1,
product: 5e09e507fcda6f268cefef40,// object ID
user: 5e0dda6d6853702038da60f0,
expireAt: 2020-01-06T14:33:55.573Z,
__v: 0
},
],
I want to match if carts array, contains cart with a product that user is adding, to not add it the second time but instead increment qty of the existing one.
My code
const itemId = req.body._id;
const userId = req.user._id;
const { qty } = req.body;
try {
const producto = await Product.findById(itemId);
const user = await User.findById(userId).populate({
path: 'carts',
});
const result = user.carts.find((o) => {
console.log(typeof o.product) // returns object
console.log(typeof producto._id); // returns object
return o.product === producto._id
});
console.log(result); // returns undefined
if (result !== undefined) {
const foundCart = await Cart.findById(result._id);
foundCart.qty += qty;
await foundCart.save();
return res.json({ message: 1 });
}
const newCart = new Cart({
qty,
product: producto,
user,
});
const cart = await newCart.save();
user.carts.push(cart);
await user.save();
return res.json({ message: 1 });
} catch (error) {
return console.log(error);
}
I think the problem is this line
return o.product === producto._id
Can you change it like this and try?
return o.product.toString() === producto._id.toString()

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