Delete comments inside array using index value using mongoose - node.js

I have to delete the second comment using index value.. Following is my document structure
{
"_id" : ObjectId("000000000fea5f24282e715b"),
"file_name" : "resume_of_ganga_.docx",
"created_date" : ISODate("2017-11-28T10:29:10.373Z"),
"updated_date" : ISODate("2017-11-28T12:39:32.148Z"),
"comments" : [
{
"created_date" : ISODate("2017-11-28T13:23:51.472Z"),
"status" : "N",
"comment_text" : "Yes...",
"username" : "name"
},
{
"created_date" : ISODate("2017-11-28T13:24:15.938Z"),
"status" : "N",
"comment_text" : "asdsd",
"username" : "name"
}
]
}
I have using this following mongoose query..But my comments are not get deleting
mongo.filemanager.findOneAndUpdate({ "_id": req.body.id},{$pull : {"'comments.' +req.body.c_index" : 1 }},function(err,response){
console.log("Deleted")
});
For example am getting index value as 2.. It should delete the second comment...
Thanks in Advance

I tried looking up to see if MongoDB has any such functionality but seems like they don't from what I found.
A workaround could be something like this. Not sure if this can be considered an atomic operation.
const removeCommentAtIndex = (index) => {
mongo.filemanager.findById(req.body.id, (err, file) => {
file.comments.splice(index, 1);
file.save();
})
}
I executed the accepted answer for In mongoDb, how do you remove an array element by its index in my test database and IT WORKS
mongo.filemanager.findOneAndUpdate({}, {"$unset" : {"comments.1" : 1 }})
mongo.filemanager.findOneAndUpdate({}, {"$pull" : {"comments" : null}})
Note that your req.body.c_index needs to be 1 to remove 2nd comment.

Related

How to add/update in array document with condition in mongoose

I need to perform the upsert operation. Below is my document structure.
I want to update the spin_details array when there is a match found with package_id but with two conditions i.e. user_id & spin_details.package_id.
If there is a match with user_id but there is no match with spin_details.package_id then some package information has to be pushed into the spin_details array. If there is no match with user_id(only) itself then it should be able to insert a new document.
{
"_id" : ObjectId("6234ffa6bd36b0e5a05ac913"),
"user_id" : ObjectId("6230e5e2b1530b407cedeb1d"),
"__v" : 0,
"is_active" : true,
"spin_details" : [
{
"package_id" : ObjectId("6230e5e2b1530b407cedeb9d"),
"spin_count" : 10,
"_id" : ObjectId("6234ffa6f390e1fafa8e215b")
},
{
"package_id" : ObjectId("6230e5e2b1530b407cedeb2a"),
"spin_count" : 25,
"_id" : ObjectId("6234ffa6f390e1fafa8e409b")
}
]
}
I can do this using multiple different queries and then based on the result value. How can I do this with a single mongoose query for this situation?

Bulk mongodb insert in Meteor or Node

MongoDB supports bulk insert http://docs.mongodb.org/manual/core/bulk-inserts/
I have tried it in Meteor collection:
Orders.insert([
{ "cust_id" : "A123", "amount" : 500, "status" : "A", "_id" : "iZXL7ewBDALpic8Fj" },
{ "cust_id" : "A123", "amount" : 250, "status" : "A", "_id" : "zNrdBAxxeNZQ2yrL9" },
{ "cust_id" : "B212", "amount" : 200, "status" : "A", "_id" : "vev3pjJ8cmiDHHxe4" },
{ "cust_id" : "A123", "amount" : 300, "status" : "D", "_id" : "BBLngRhS76DgeHJQJ" }
]);
but it creates just
{ "0" : { "cust_id" : "A123", "amount" : 500, "status" : "A", "_id" : "iZXL7ewBDALpic8Fj"},
"1" : { "cust_id" : "A123", "amount" : 250, "status" : "A", "_id" : "zNrdBAxxeNZQ2yrL9" },
"2" : { "cust_id" : "B212", "amount" : 200, "status" : "A", "_id" : "vev3pjJ8cmiDHHxe4" },
"3" : { "cust_id" : "A123", "amount" : 300, "status" : "D", "_id" : "BBLngRhS76DgeHJQJ" },
"_id" : "6zWayeGtQCdfS65Tz" }
I need it for performance testing purposes. I need to fill and test database with thousands of testing items. I do inserts in foreach, but it takes too long to fill database.
Is here any workaround? Or can we expect Meteor will support this in next versions?
You could use exec (nodejs docs) to run a mongo script inside of meteor inside of a Meteor.startup on the server.
Example:
Meteor.startup(function () {
var exec = Npm.require('child_process').exec;
exec('mongo localhost:27017/meteor path-to/my-insert-script.js', function ( ) {
// done
});
});
Not optimum, but I think it's your best bet for now. You can also use the command line option --eval against Mongo in exec and pass the insert statement as a string to exec. That might look like this:
Meteor.startup(function () {
var exec = Npm.require('child_process').exec;
exec('mongo localhost:27017/meteor --eval \'db.Orders.insert(' + JSON.stringify(arrOfOrders) + ')\'', function ( ) {
// done
});
});
When inserting a lot of data into the DB, e.g., in a forEach loop, you want to make sure that there is no reactive content on your page that depends on it. Otherwise the reactive rerendering is going to slow your client down tremendously. You can easily insert several thousand document into a collection in a fraction of a second when all templates are disabled, while the same operation can take several minutes with your CPU at 100% on both the client as the server if there is relevant reactivity happening.
You may want to add a condition to any template whose content depend on this data such as:
Template.myTemplate.items = function() {
if (Session.get("active")) {
return Order.find();
}
}
Then you can deactivate all reactive rerendering before your forEach loop and reactivate it again afterwards (Session.set("active", false)).
You could use rawCollection which is node mongodb driver implemetation in Meteor.Collection.
await Orders.rawCollection().insertMany(arrOfOrders)
It works on 70M data in my case.
(await makes it synced so you should consider to use it or not for your purpose.)

How to delete a subdocument from an array of documents where <condition>?

I thought I had gotten quite comfortable with mongodb until I ran in to the following issue. I have a collection of documents in the following format:
{
"_id" : ObjectId("4f876104d976649cbb1f6cf2"),
"course_list" : [
{
"course_id" : "AL101",
"Grade" : "A"
},
{
"course_id" : "PS101",
"Grade" : "B"
},
{
"course_id" : "EL101",
"Grade" : "B"
}
],
"user_name" : "jim"
}
I want to delete all sub-documents from the array "course_list" where "Grade" is NOT EQUAL to "A".
I tried a bunch of different queries, but nothing worked. But then, I thought the following would work for sure, but it didnt either:
db.courses.update({'user_name' : 'jim'}, {$pull : {'course_list' : {'Grade' : {$ne : 'A'}}}})
I get the following array when I run the above command:
Cannot apply $pull/$pullAll modifier to non-array
Can anyone point me in the right direction, please?
Thanks for your comment. My query was indeed correct. The error, as you rightly pointed out, was because of a document where course_list wasn't an array.

How can i retrieve a subrecord from mongo DB

I have a db that looks like this :
{
"_id" : ObjectId("50525e55467f67da3f8baf06"),
"client" : "client1"
}
{
"_id" : ObjectId("505262f0deed32a758410b1c"),
"client" : "client2"
}
{
"_id" : ObjectId("5052fe0a37083fd981616589"),
"client" : "client3",
"products" : [
{"name" : "product1"},
{"name" : "product2"}
]
}
How can i retrieve the product list of client3 without retrieving the client3 record ?
The output should look like this :
[
{"name" : "product1"},
{"name" : "product2"}
]
I don't think you can completely exclude the client3 record as the products are part of this record, but you can select just the products field like this:
db.<dbname>.find({ 'client' : 'client3' }, { 'products' : 1, '_id' : 0 })
Also - if you want to get just the matching subrecord - see here
http://docs.mongodb.org/manual/reference/operator/projection/positional/
you use the $ operator in the project portion of find to specify the n'th subrecord where that is the first to match your query.

updating nested document with mongoDb + nodeJs

I have a structure like this:
{
"_id" : ObjectId("501abaa341021dc3a1d0c70c"),
"name" : "prova",
"idDj" : "1",
"list" : [
{
"id" : 1,
"votes" : 2
},
{
"id" : 2,
"votes" : 4
}
]
}
And I'm trying to increase votes with this query:
session_collection.update(
{'_id':session_collection.db.bson_serializer.ObjectID.createFromHexString(idSession),'list.id':idSong},
{$inc:{'list.$.votes':1}},
{safe: true} ,
callback);
But it doesn't work, there are no problems it just doesn't update anything.
I think it's because the ['] (simple quotation mark) on the 'list.id' and 'list.$.votes' because the same query inside the terminal works perfectly.
Thanks!
I suspect your matching is not working as you expect. The callback will return
function(err, numberofItemsUpdated, wholeUpdateObject)
numberofItemsUpdated should equal 1 if your matching worked. you'll need to check if idSession and idSong are what you think they are.

Resources