Update creates a duplicate with exact same _id
shivardev opened this issue · comments
Shiva_Dev commented
I tried to update a document which already exists
This is the function
async function appendChat(chatData: ChatContent) {
try {
const db = new Datastore({
filename: "src/Db/UserChats.db",
autoload: true,
});
const condition = { from: chatData.from, to: chatData.to };
db.findOne(condition, (err: Error | null, userChat: any) => {
if (err) {
console.error("Error while searching for userChat:", err);
} else if (!userChat) {
// If userChat doesn't exist, create a new one
const newUserChat: TChat = {
from: chatData.from,
to: chatData.to,
ChatContent: [chatData],
// Add other properties as needed
};
// Insert the new userChat
db.insert(
newUserChat,
(insertErr: Error | null, insertedUserChat: Chat) => {
if (insertErr) {
console.error("Error while inserting userChat:", insertErr);
} else {
console.log("New userChat created:", insertedUserChat);
}
}
);
} else {
console.log("UserChat already exists:", userChat);
console.log(userChat._id);
db.update(
{ _id: userChat._id },
{ $push: { ChatContent: chatData } },
{},
(updateErr: Error | null, numUpdated: Chat) => {
if (updateErr) {
console.error("Error while updating userChat:", updateErr);
} else {
console.log("UserChat updated with new content:", numUpdated);
}
}
);
}
});
} catch (err) {
console.error("Error hashing password:", err);
}
}
instead of updating the one that exists already. it creates a new object and saves it same _id
{
"from": "EmailSample007@gmail.com",
"to": "EmailSample@gmail.com",
"ChatContent": [
{
"audioURL": "uploads/audios/1697701185128-w2g0y7-audiofile.m4a",
"from": "EmailSample007@gmail.com",
"to": "EmailSample@gmail.com",
"isVoice": true,
"msg": ""
}
],
"_id": "ilypBnrGLe4bITnH"
}
{
"from": "EmailSample007@gmail.com",
"to": "EmailSample@gmail.com",
"ChatContent": [
{
"audioURL": "uploads/audios/1697701185128-w2g0y7-audiofile.m4a",
"from": "EmailSample007@gmail.com",
"to": "EmailSample@gmail.com",
"isVoice": true,
"msg": ""
},
{
"audioURL": "uploads/audios/1697701435156-td7c1b-audiofile.m4a",
"from": "EmailSample007@gmail.com",
"to": "EmailSample@gmail.com",
"isVoice": true,
"msg": ""
}
],
"_id": "ilypBnrGLe4bITnH"
}
Please help me solve it. Thankyou
jeking commented
The same to me,help
Shiva_Dev commented
i didn't get any solution rather I have dropped using the NEDB and moved to MongoDB local,
Shiva_Dev commented
The same to me,help
As this version is not maintained or supported maybe try using the forked supported version
https://github.com/seald/nedb