mongoDB: Update nested array object - javascript

I would like the nested object with the id BAHx9KeKjuMePce6f to be updated:
{
"_id" : "sgG6G9XTvvjj7uxwQ",
"target" : [
{
"title" : "content",
"id" : "ePce6fBAHx9KeKjuM"
},
{
"title" : "content",
"id" : "BAHx9KeKjuMePce6f" <--
}
]
}
So this is what I tried:
var newData = { title: "new", one: "more", id: 'BAHx9KeKjuMePce6f' };
Collection.update(
{ _id: 'sgG6G9XTvvjj7uxwQ', 'target.id': 'BAHx9KeKjuMePce6f' },
{ $set: newData }
);
The result should be:
{
"_id" : "sgG6G9XTvvjj7uxwQ",
"target" : [
{
"title" : "content",
"id" : "ePce6fBAHx9KeKjuM"
},
{
"title": "new",
"one": "more",
"id" : "BAHx9KeKjuMePce6f"
}
]
}

In order to update specific element in array you can use mongodb positional $ operator.
Try the following query:
var newData = { title: "new", one: "more", id: 'BAHx9KeKjuMePce6f' };
Collection.update(
{ _id: 'sgG6G9XTvvjj7uxwQ', 'target.id': 'BAHx9KeKjuMePce6f' },
{ $set: { 'target.$': newData } }
);

You need the use the positional parameter $ to indicate you want to update the array element, rather than the root of the document, see the documentation:
Collection.update({
_id: 'sgG6G9XTvvjj7uxwQ',
'target.id': 'BAHx9KeKjuMePce6f'
}, {
$set: {
"target.$": newData
}
});

Related

how to avoid duplicate key error collection in mongodb

i want to build a cart for my website, this is the schema for the cart:
const productSchema = require("./product")[1];
const cartItemSchema = new Schema<CartItem>(
{
product: productSchema,
quantity: {
type: Number,
required: true,
min: [1, "Quantity can not be less then 1."],
},
},
{
timestamps: true,
}
);
const CartSchema = new Schema(
{
userID: {
type: Schema.Types.ObjectId,
ref: "User",
},
items: [cartItemSchema],
},
{ timestamps: true }
);
module.exports = model<Cart>("Cart", CartSchema);
the problem is, when I add a product in a specific user cart, while the same product is allready added to another user cart document, I get this error:
"message":"cannot add to cart E11000 duplicate key error collection: elec-store.carts index: items.productID_1 dup key: { items.productID: null }, stack: MongoError: E11000 duplicate key error collection: elec-store.carts index: items.productID_1 dup key
this is the add function
public async add(cartItem: CartItem, userID: string): Promise<Cart> {
let cartInDB = null;
await CartModel.findOne({ userID: userID }, (err, cart) => {
cartInDB = cart;
});
if (AppUtils.hasValue(cartInDB)) {
const index = cartInDB.items.findIndex(
(item) => item.product._id.toString() === cartItem.product._id
);
if (index !== -1) {
cartInDB.items[index].quantity =
cartInDB.items[index].quantity + cartItem.quantity;
cartInDB.items[index].product._id = cartItem.product._id;
const cartAfterAdding = await cartInDB.save();
return cartAfterAdding;
} else {
await CartModel.update(
{ _id: cartInDB._id },
{ $push: { items: cartItem } }
);
}
return cartInDB;
} else {
const itemsArray: CartItem[] = [];
itemsArray.push(cartItem);
let createdCart = new CartModel({
userID: userID,
items: itemsArray,
});
await createdCart.save(); \\ this is where the problem occurs
return createdCart;
}
}
and this is how my cart looks like in mongodb document:
db.carts.find().pretty()
{
"_id" : ObjectId("60ea9fb81b2b4c048c3b1544"),
"userID" : ObjectId("60dee5e1da81bd274cd304de"),
"items" : [
{
"_id" : ObjectId("60ea9fb81b2b4c048c3b1545"),
"product" : {
"_id" : ObjectId("60e62cb21f74572b7c0b3a30"),
"name" : "tv",
"description" : "the best tv",
"categoryID" : 2,
"quantity" : "2",
"serialNumber" : "226swaq12",
"price" : 2000,
"imgUrl" : "https://www.seekpng.com/png/full/774-7744281_samsung-electronics-samsung-electronic-product-png.png"
},
"quantity" : 6,
"createdAt" : ISODate("2021-07-11T07:37:29.790Z"),
"updatedAt" : ISODate("2021-07-11T07:38:15.583Z")
},
{
"_id" : ObjectId("60eaa16b1b2b4c048c3b155d"),
"product" : {
"_id" : ObjectId("60e066009be1060748201ad3"),
"name" : "samsung tv",
"description" : "the best tv",
"quantity" : "2",
"categoryID" : 2,
"serialNumber" : "2212",
"price" : 2000,
"imgUrl" : "https://www.seekpng.com/png/full/774-7744281_samsung-electronics-samsung-electronic-product-png.png"
},
"quantity" : 9,
"updatedAt" : ISODate("2021-07-11T07:46:19.313Z"),
"createdAt" : ISODate("2021-07-11T07:44:43.764Z")
}
],
"createdAt" : ISODate("2021-07-11T07:37:29.792Z"),
"updatedAt" : ISODate("2021-07-11T07:46:19.314Z"),
"__v" : 0
}
I use mongoose.Schema to create new schemas and then when making reference to a different schema I do it like this:
product: { type: mongoose.Schema.Types.ObjectId, ref: 'product' },
If later you need to show also the product info (db.carts.find()), you can use populate() to replace the reference for all the product entries.
You can use upsert true.
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string> // Available starting in MongoDB 4.2
}
)
For example -
db.books.update(
{ item: "ZZZ135" }, // Query parameter
{ // Replacement document
item: "ZZZ135",
stock: 5,
tags: [ "database" ]
},
{ upsert: true } // Options
)
This may help: Mongo Update

Push an object to an array which is a field of an object in a array Mongoose

Basically I have the following schema.
{
...,
description: {
type: String,
required: true,
trim: true
},
tags: {
type: [{
type: String
}]
},
lessons: [{
name: String,
description: String,
video_path: String,
comments: [
{
user: mongoose.Schema.ObjectId,
content: String,
createdAt: {
type: Date,
default: Date.now
}
}
]
}]
,
createdAt: {
type: Date
}
}
I want to insert the following object to the comments array of a lesson object when the id of the lesson object is given.
{
userId: "5e1b4790f7a3ca42accfeed3",
content: "First comment"
}
The following is what I have tried. However it doesn't throw any error, but it's not inserting any comments to the DB also. Thanks for any helpful advice.
addComment: async (courseId, lessonId, userId, content, callback) => {
Course.update(
{ _id: courseId, "lessons._id": lessonId },
{
$push: {
comments: {
user: userId,
content: content
}
}
},
function(err, data) {
if (err) {
console.log(err);
return callback(err, null);
} else {
console.log(data);
return callback(null, data);
}
}
);
}
EDIT:
Collection data:
{
"_id" : ObjectId("5e1b4790f7a3ca42accfeed3"),
"tags" : [ "mathematics", "beginner", "fundamentals" ],
"name" : "Mathematics Toobox",
"description" : "Mathematics includes the study of such topics as quantity (number theory), structure (algebra), space (geometry), and change (mathematical analysis).",
"price" : 1500,
"lessons" : [
{
"_id" : ObjectId("5e1b48d9f7a3ca42accfeed4"),
"name" : "Welcome to the course",
"description" : "Welcome to Mathematics Toolbox course\n I’ll be your instructor for this course that runs for xx weeks ending on XXXXX.\n1. Access the technology tutorial located on your My Home page if you are new to the learning Hub, this online learning management system.",
"video_path" : "uploads\\1578846427336-Shakira - Hips Don't Lie ft. Wyclef Jean.mp4"
},
{
"_id" : ObjectId("5e1e8f80cf166a2cb82b7a5e"),
"name" : "Number system",
"description" : "Baby just love me love me love me\nBaby just hold me hold me hold me\nOh love me ",
"video_path" : "uploads\\1579061121969-Ellis - Migraine (feat. Anna Yvette) [NCS Release].mp4"
}
],
"createdAt" : ISODate("2020-01-12T16:21:36.778Z"),
"__v" : 0,
"cover_path" : "uploads\\1578846099107-img_4.jpg"
}
There are a few problems in your schema.
I think you want to have an array of string tags.
Also you need to use ref property to make a reference to the User model.
So schema must be updated like this:
(I assume that you used User in model creation.)
const mongoose = require("mongoose");
const courseSchema = new mongoose.Schema({
description: {
type: String,
required: true,
trim: true
},
tags: {
type: [String]
},
lessons: [
{
name: String,
description: String,
video_path: String,
comments: [
{
user: {
type: mongoose.Schema.ObjectId,
ref: "User"
},
content: String,
createdAt: {
type: Date,
default: Date.now
}
}
]
}
],
createdAt: {
type: Date
}
});
module.exports = mongoose.model("Course", courseSchema);
Now you can use findByIdAndUpdate method with push and filtered positional operator $.
to add a comment like this:
Course.findByIdAndUpdate(
{ _id: courseId },
{
$push: { "lessons.$[lesson].comments": { user: userId, content } }
},
{
arrayFilters: [{ "lesson._id": lessonId }],
new: true
},
function(err, data) {
if (err) {
console.log(err);
return callback(err, null);
} else {
console.log(data);
return callback(null, data);
}
}
);
Test:
Let's say you have an user with _id: 5e20954dc6e29d1b182761c9, and a course like this:
{
"tags": [
"tag1",
"tag2"
],
"_id": "5e209631a90e651e9c238df2",
"description": "description1",
"lessons": [
{
"comments": [],
"_id": "5e209631a90e651e9c238df3",
"name": "lesson1 name",
"description": "lesson1 description",
"video_path": "lesson1 video_path"
}
],
}
When you send a comment with these values:
courseId = "5e209631a90e651e9c238df2",
lessonId = "5e209631a90e651e9c238df3",
userId = "5e20954dc6e29d1b182761c9",
content = "Comment Content"
The result will be:
{
"_id" : ObjectId("5e209631a90e651e9c238df2"),
"tags" : [
"tag1",
"tag2"
],
"description" : "description1",
"lessons" : [
{
"comments" : [
{
"_id" : ObjectId("5e2099799edf132a08c2b997"),
"user" : ObjectId("5e20954dc6e29d1b182761c9"),
"content" : "Comment Content",
"createdAt" : ISODate("2020-01-16T20:12:25.243+03:00")
}
],
"_id" : ObjectId("5e209631a90e651e9c238df3"),
"name" : "lesson1 name",
"description" : "lesson1 description",
"video_path" : "lesson1 video_path"
}
]
}
This is what finally worked for me.
const newData = {
'lessons.$.comments': {
user: userId,
content: content
}
}
Course.updateOne({_id: courseId,'lessons._id': lessonId}, {'$push':
newData
}, function(err,num) {
console.log(num)
if(num.nModified > 0){
callback(null,num)
}
else if(err){
callback(err,null)
}
})
}

Group collection based on the common values of a field mongodb

My collection looks like this
{
"email" : "tp#sd.com",
"subscriptions" : [
{
"default" : false,
"interval" : "weekly",
"tags" : [
{
"name" : "group-1",
"value" : "g1-value-1"
},
{
"name" : "group-1",
"value" : "g1-value-2"
},
{
"name" : "group-2",
"value" : "g2-value-1"
},
{
"name" : "group-3",
"value" : "g3-value-1"
},
{
"name" : "group-3",
"value" : "g3-value-2"
}
]
},
{
"email":"lol#xyz.com",
"subscriptions" : [
{
"default" : false,
"interval" : "weekly",
"tags" : [
{
"name" : "group-1",
"value" : "g1-value-2"
},
{
"name" : "group-1",
"value" : "g1-value-1"
},
{
"name" : "group-2",
"value" : "g2-value-1"
},
{
"name" : "group-2",
"value" : "g2-value-3"
},
{
"name" : "group-3",
"value" : "g3-value-1"
}
]
}
}
I want to group this to an array of objects which contain emails and common group values.
for example, A user-1 has g1-value-1 and user-2 also has g1-value-1, maybe along with other values as well. But these users should be grouped together along with their tags. How do I achieve this?
Surprisingly it's quite an easy query:
db.collection.aggregate([
{
$unwind: "$subscriptions"
},
{
$unwind: "$subscriptions.tags"
},
{
$group: {
_id: "$subscriptions.tags.value",
emails: {$addToSet: "$email"}
}
}
]);
The result will look like this:
[
{
_id: "g1-value-1",
emails: [
"tp#sd.com",
"lol#xyz.com"
]
},
{
_id: "g1-value-2",
emails: [
"tp#sd.com",
"lol#xyz.com"
]
},
{
_id: "g2-value-1",
emails: [
"tp#sd.com",
"lol#xyz.com"
]
},
{
_id: "g2-value-3",
emails: [
"lol#xyz.com"
]
},
{
_id: "g3-value-1",
emails: [
"tp#sd.com",
"lol#xyz.com"
]
},
{
_id: "g3-value-2",
emails: [
"tp#sd.com",
]
},
]

mongodb pull object from an array not working using mongoose

I am trying to remove a comment object from an array in a mongodb using the $pull operator and it seems like I have the syntax correct but it is not modifying anything.
I have looked through all the examples given on Stack to make but it still keeps responding with
{ n: 0,
nModified: 0,
opTime:
{ ts:
Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1548664023 },
t: 1 },
electionId: 7fffffff0000000000000001,
ok: 1,
operationTime:
Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1548664023 },
'$clusterTime':
{ clusterTime:
Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1548664023 },
signature: { hash: [Binary], keyId: [Long] } } }
this is the field I currently have in the DB
{
"_id" : ObjectId("5be23d8aa365d853ddfd6f15"),
"__v" : 0,
"restaurant" : {
info about restaurant
},
"comments" : [
{
"id" : "61DSLu7fFcUZ2chA8-A6HQ",
"user" : "test",
"comment" : "test"
},
{
"comment" : "testing",
"user" : "testing",
"id" : ObjectId("5c3cd3a5647f180484a5ca18")
},
{
"restaurant_id" : "61DSLu7fFcUZ2chA8-A6HQ",
"comment" : "tacos",
"name" : "test",
"user_id" : ObjectId("5c48fdf47e9ed81b08536602")
},
{
"restaurant_id" : "61DSLu7fFcUZ2chA8-A6HQ",
"comment" : "tacos",
"name" : "test",
"comm_id" : ObjectId("5c49019f8528f31b2adfb914")
},
{
"restaurant_id" : "61DSLu7fFcUZ2chA8-A6HQ",
"comment" : "hello",
"name" : "test",
"comm_id" : ObjectId("5c490237fd6e781b52f801fe")
}
],
"likes" : {
"likes" : 6
}
Currently my model shows within my restaurants model
comments: [{
restaurant_id : String,
comment : String,
name : String,
comm_id : String,
}]
the update method I have currently
db.restaurants.updateOne({restaurant_id: rest_id},
{ $pull: { comments: { $in: [{comment: "hello"}] } }
}, { safe: true })
and also have tried
db.restaurants.updateOne({restaurant_id: rest_id},
{ $pull: { comments: { $in: {"comment": "hello"} } }
}, { safe: true })
as well as
db.restaurants.updateOne({restaurant_id: rest_id},
{ $pull: { comments: { comment: "hello"} } }
}, { safe: true })
and similar variation. I can't seem to pinpoint my mistake. the response seems like it is finding the correct restaurant field, but my $pull operator just isn't working properly. Is there something wrong with my syntax or does it not work in this scenario.
Ideally, I will use the comm_id field to remove the object from the array, but I am using comment: "hello" just to test.
Is it possibly because I have different fields in the first few comments?
do it simple it will works
db.restaurants.updateOne({restaurant_id: rest_id},
{ $pull: { comments.comment: "hello"} }
}, { safe: true })

How to remove element in mongodb array

How can I remove just one element of an array in a collection like this:
{
"_id" : "Y9BBFa4c4vMiAkjbi",
"metadata" : {
"tags" : [
"Anything",
"Something",
"More"
]
}
}
In this example I just want to remove 'Something' - if it is existing.
var tag = 'Something';
if (Collection.find({ 'metadata.tags': tag }).count()) {
Collection.update(
{ _id: id },
{ $pull: { 'metadata.tags': tag } }
);
}
Just make sure your $pulltargets the array element you want to remove:
Collection.update({ _id: id },
{ $pull: { 'metadata.tags': { $eq: "Something" }}}
);

Categories