mongodb pull object from an array not working using mongoose - javascript

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 })

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

Mongodb aggregate by using input array

I am struggling to create an aggregate query for my MongoDB database.
here is my input array
recipients = [1,2,7]
here is my database collection
{
"chapter": 1,
"targets": [
{
type: 'user',
recipient: 1
}
]
},
{
"chapter": 1,
"targets": [
{
type: 'user',
recipient: 2
}
]
},
{
"chapter": 2,
"targets": [
{
type: 'user',
recipient: 3
}
]
},
{
"chapter": 3,
"targets": [
{
type: 'user',
recipient: 4
}
]
},
the desired output
should be [] because 7 doesn't exist in targets.recipient in the collection
here is what I've tried so far
db.collection.aggregate([
{
$match: {
'targets.recipient': { $in: recipients },
},
}
])
Any suggestions, thank you.
The way $in works is that it returns the document if there's any match between it's value and the array you're passing as a parameter. It looks like in your case you can use $in for initial filtering but then you want to return the result only if the result set contains all the values from the input array. In order to achieve it you can use $group to get all matching results and then apply $all:
db.collection.aggregate([
{
$match: { "targets.recipient": { $in: [1,2,7] } }
},
{
$group: {
_id: null,
docs: { $push: "$$ROOT" }
}
},
{
$match: { "docs.targets.recipient": { $all: [1,2,7] } }
}
])
Mongo Playground
// only matched documents will be shown.
> db.targets.aggregate([ {$match:{"targets.recipient":{$in:[1,2,7]}}}, {$project:{chapter:1,targets:1}} ]).pretty();
{
"_id" : ObjectId("5f5de14c598d922a1e6eff4d"),
"chapter" : 1,
"targets" : [
{
"type" : "user",
"recipient" : 1
}
]
}
{
"_id" : ObjectId("5f5de14c598d922a1e6eff4e"),
"chapter" : 1,
"targets" : [
{
"type" : "user",
"recipient" : 2
}
]
}
>

why is mongoose TTL deleting document, even when partial filter does not match the option

my partial filter is deleting document, but user is not matching that requirement, am I using partial filter incorectly?
Thanks
const postSchema = new mongoose.Schema(
{
title: { type: String },
description: { type: String },
image: { type: String },
price: { type: String },
location: { type: String },
image: { type: Array },
author: {
type: String,
ref: 'User'
},
authorPremium: {
type: Boolean,
default: true,
index:true
},
reported: {
type: Boolean,
default: false
},
reportClear: {
type: Boolean,
default: false
}
},
{
timestamps: true
}
);
// users who are not premium will have posts deleted after 20 seconds
postSchema.index({ createdAt: 1 }, { expireAfterSeconds: 20, partialFilterExpression: { authorPremium: false } });
module.exports = mongoose.model('Post', postSchema);
partial filer should not allow the authorPremium which is true to be deleted, but only delete is authorPremium is false... please advise.
return from mongo index
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.posts"
},
{
"v" : 2,
"key" : {
"createdAt" : 1
},
"name" : "createdAt_1",
"ns" : "test.posts",
"expireAfterSeconds" : 120,
"background" : true
},
{
"v" : 2,
"key" : {
"authorPremium" : 1
},
"name" : "authorPremium_1",
"ns" : "test.posts",
"background" : true
},
{
"v" : 2,
"key" : {
"timestamps" : 1
},
"name" : "timestamps_1",
"ns" : "test.posts",
"expireAfterSeconds" : 20,
"background" : true
}
]
it seems when I use mongo cmd some of my old setting remained.. and some new? So how can I completly clear these old ttl settings when I am testing and ensure only the ones I want are there?
It looks like your .index(...) does not work because you already have old index on createdAt field and mongoose won't drop the old one. To synchronize the indexes with your schema you can use Model.syncIndexes
More on how .index() works and why .syncIndexes() was introduced can be found here.

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)
}
})
}

mongoDB: Update nested array object

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
}
});

Categories