How to add new value with same key to document - javascript

I need to write down a category and its cost in category object. Now, in my code example, I am adding the products: $50 category to the user. After execution, 'categories': { products: '50$' } is displayed in the console. But if you add products: 1000$ again, it will overwrite and output 'categories': { products: '1000$' }. And I need to display 'categories': { products: '$50' , products: '$1000' }. How can this be implemented?
mongoClient.connect(function (err, client) {
if (err) return console.log(err);
const db = client.db("db");
const col = db.collection("coll");
col.findOneAndUpdate(
{
name: "User1",
},
{
$set: {
"сategories.products": "50$",
},
},
function (err, result) {
console.log(result);
client.close();
}
);
});

Instead of
$set: {
"сategories.products": "50$"
}
use
$addToSet: {
сategories: {
products: "1000$"
}
}

Well if I understood you correctly you have an object like categories:{products:$50} but you want that whenever you post a new price it should get added to the object not overwrite it.
I think that is not possible either every time you create a new object like {products1:$50, products2:$1000} and set your categories field to be equal to this new object
OR
you should make your model like categories:{products:[]}. So, that you can just push the new price in the array.

Related

How to remove data in object from MongoDB document

The object contains the username and category. Categories is another object that contains categories and costs. How can I remove all key-values ​​in a category object? Now in my code, using deleteOne() I find all the fields of the object named "Bob". I am deleting the entire document named "Bob". And I need to clear the categories in this document, for example, deleteOne({ name: "Bob", category }, and have { name: "Bob", category: {} } output to the console
let users = [
{
name: "Bob",
сategories: {
eat: "20$",
entertainment: "100$",
},
},
];
mongoClient.connect(function (err, client) {
if (err) return console.log(err);
const db = client.db("expensesdb");
db.collection("users").deleteOne({ name: "Bob" }, function (err, result) {
console.log(result);
client.close();
});
});
You can use updateOne method to set сategories as empty object using $set or you can use $unset to unset your сategories data.

How to delete items out of a nested array by their _id in Mongoose

I have a Model, Users, and each user has an array of objects called habits.
{
_id: 606f1d67aa1d5734c494bf0a,
name: 'Courtney',
email: 'c#gmail.com',
password: '$2b$10$WQ22pIiwD8yDvRhdQ0olBe6JnnFqV2WOsC0cD/FkV4g7LPtUOpx1C',
__v: 35,
habits: [
{
_id: 6081d32580bfac579446eb81,
name: 'first',
type: 'good',
days: 0,
checked: false
},
{
_id: 6081d32f80bfac579446eb82,
name: 'seconds',
type: 'bad',
days: 0,
checked: false
},
]
}
From my client side, I send over a list of ids of the habits I want to delete out of the array, that looks like this..
[
'6081d32580bfac579446eb81',
'6081d32f80bfac579446eb82',
]
I am trying to find a way to delete the IDs in the habits array, deleting only the habits whose IDs are sent in the array above.
Here is what I have tried....
router.post('/delete', validate, async (req, res) =>{
const user = await User.findById(req.user._id)
const idList = req.body.ids.checkedItems
const updatedList = user['habits'].filter(habit=> {
return !idList.includes(`${habit._id}`)
})
user['habits'] = updatedList;
try {
await user.save()
res.send(updatedList)
} catch (err) {
res.status(400).send(err)
}
})
-idList is the array of ids as strings.
-user['habits'] accesses the list from the document, user.
-my filter method only returns the habits that are NOT included in the idList array. Because the Ids in the array are the ones to be deleted.
This solution is obviously just vanilla javascript, what I am looking for is if anyone knows how to achieve this using mongoose.js syntax.
I think you could do this by using deleteMany or deleteOne on the Model, but I am not sure how to achieve this.
Thank you for taking the time to help or give suggestions.
The solution that worked for me in the end is to use the Model method 'findByIdAndUpdate'.
const { itemsToDelete } = req.body
User.findByIdAndUpdate(req.user._id,
{ $pull: { habits: { _id: itemsToDelete } } },
{ new: true , useFindAndModify: false},
function (err, data) {
if (err) {
res.send(err)
} else {
res.send(data.habits)
}
}
)
You can use a combination of $pull and $in for this.
User.update({_id: userId},
{
$pull: {
habits: {
_id: {
$in: [
ObjectId("6081d32580bfac579446eb81"),
ObjectId("6081d32f80bfac579446eb82")
]
}
}
}
})

Mongo / Mongoose - Push object into document property array on condition

I have a document stored on mongo that, amongst others, contains a property type of an array with a bunch of objects inside. I am trying to push an object to that array. However each object that gets pushed to into the array contains a date object. How I need it to work is if an object with that date already exists then dont push the new object into the array.
I'm been playing with it for a while, trying to run update and elemMatch queries etc, but I can't figure it out. I have a long draw out version below, as you can see I'm making 2 separate requests to the database, can someone help me reduce this to something simpler?
UserModel.findById(req.user._id, (err, doc) => {
if (err) {
return res.sendStatus(401);
}
const dateExists = doc.entries.find((entry) => entry.date === date);
if (dateExists) {
res.sendStatus(401);
} else {
UserModel.findByIdAndUpdate(
req.user._id,
{
$push: {
entries: {
date,
memo,
},
},
},
{ safe: true, upsert: true, new: true },
(err, doc) => (err ? res.sendStatus(401) : res.sendStatus(200))
);
}
});
Thanks.
db.users.updateOne({
_id: req.user._id,
"entries.date": date,
},
{
$push: {
entries: {date,memo},
}
})
https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/index.html

How can I use transactions to check if an item exists in one table before updating a second table using AWS.DynamoDB.DocumentClient?

I am new to DynamoDB. I have a DynamoDB table called 'kids-profiles' that lists child profiles for a user (primary partition key 'userId', primary sort key 'childId'). I have a second table called 'kids-tasks' that lists tasks for a child (primary partition key 'childId', primary sort key 'taskId'). I would like to atomically add an item to the 'kids-tasks' table only if the 'childId' exists in the 'kids-profiles' table.
I am trying to use the transactWrite method of the AWS.DynamoDB.DocumentClient class (https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html) to achieve this, but I can't get it to work. First, when I try to add a ConditionCheck to the TransactionItems, it says "TransactItems can only contain one of Check, Put, Update or Delete". So I have changed "ConditionCheck" to "Check" instead, assuming that is correct even though it is not mentioned in the documentation. Second, when I do get it to execute, it adds the task to the 'kids-tasks' table regardless of whether or not the 'childId' exists in the 'kids-profiles' table.
const params = {
TransactItems: [
{
Check: {
TableName: "kids-profiles",
Key: {
userId: userId,
childId: childId,
},
ConditionExpression: "attribute_exists(childId)",
},
Put: {
TableName: "kids-tasks",
Item: {
childId: childId,
taskId: uuid.v1(),
title: title,
}
}
}
]
};
try
{
await dynamoDb.transactWrite(params).promise();
console.log("Success!");
}
catch (error)
{
console.log("Error");
}
Divide objects into individual Operations.
const params = {
TransactItems: [
{
ConditionCheck: {
...
},
+ }, // <------ !!!!!
+ {
Put: {
...
}
]
};

Node.js - Mongoose - Update nested array with all values in req.body

I have an object that looks like this.
{
_id: '577fe7a842c9b447',
name: 'Jacob\'s Bronze Badges',
competitors: [
{
_id: '577fe7a842c9bd6d',
name: 'Peter\'s Silver Badges',
sites: [
{
_id: '577fe7a842c9bd6d',
name: 'Facebook',
url: 'fb.com/peter'
},
{
_id: '577fe7a842c9bd6d'
name: 'Google',
url: 'google.com/peter'
}
]
},
{
_id: '599fe7a842c9bd6d',
name: 'Paul\'s Gold Badges',
sites: [
{
'_id': '577fe7a842c9bd6d',
name: 'Facebook',
url: 'fb.com/paul'
},
{
_id: '577fe7a842c9bd6d',
name: 'Google',
url: 'google.com/paul'
}
]
}
]
}
My goal is to reference the competitors array and update items inside with all of the values from req.body. I based this code off of this answer, as well as this other one.
Location.update(
{ 'competitors._id': req.params.competitorId, },
{ $set: { 'competitors.$': req.body, }, },
(err, result) => {
if (err) {
res.status(500)
.json({ error: 'Unable to update competitor.', });
} else {
res.status(200)
.json(result);
}
}
);
I send my HTTP PUT to localhost:3000/competitors/577fe7a842c9bd6d to update Peter's Silver Badges. The request body is:
{
"name": "McDonald's"
}
The problem is that when I use $set to set the competitor with _id: req.params.competitorId, I don't know what is in req.body. I want to use the entire req.body to update the object in the array, but when I do, that object is overwritten, so instead of getting a new name, Peter's Silver Badges becomes:
{
name: 'McDonald\'s',
sites: []
}
How can I update an object within an array when I know the object's _id with all of the fields from req.body without removing fields that I want to keep?
I believe that the sites array is empty because the object was reinitialized. In my schema I have sites: [sitesSchema] to initialize it. So I am assuming that the whole competitors[_id] object is getting overwritten with the new name and then the sites: [sitesSchema] from myschema.
You would need to use the $ positional operator in your $set. In order to assign those properties dynamically, based on what is in your req.body, you would need to build up your $set programmatically.
If you want to update the name you would do the following:
Location.update(
{ 'competitors._id': req.params.competitorId },
{ $set: { 'competitors.$.name': req.body.name }},
(err, result) => {
if (err) {
res.status(500)
.json({ error: 'Unable to update competitor.', });
} else {
res.status(200)
.json(result);
}
}
);
One way you might programatically build up the $set using req.body is by doing the following:
let updateObj = {$set: {}};
for(var param in req.body) {
updateObj.$set['competitors.$.'+param] = req.body[param];
}
See this answer for more details.
To update embedded document with $ operator, in most of the cases, you have to use dot notation on the $ operator.
Location.update(
{ _id: '577fe7a842c9b447', 'competitors._id': req.params.competitorId, },
{ $set: { 'competitors.$.name': req.body.name, }, },
(err, result) => {
if (err) {
res.status(500)
.json({ error: 'Unable to update competitor.', });
} else {
res.status(200)
.json(result);
}
}
);

Categories

Resources