mongo query to delete one doc with multiple conditions in nodejs - node.js

db.LessonPlanConfiguration.deleteOne({'boardId' : ObjectId('5c270eec6c6d78001a206916') ,'gradeId' : ObjectId('5bc56a319fe946001a35d5d6') , 'topicId' : ObjectId('5c89de97d94fde0013b4983e')})
sample document :-
{
"_id" : ObjectId("5ca654a5c60baf0017130d26"),
"instituteId" : ObjectId("5bdfdb4bfdd9360013c5f063"),
"academicSessionId" : ObjectId("5c271edf6c6d78001a209880"),
"branchId" : ObjectId("5be6655120a8040014ddae80"),
"learningLevelId" : ObjectId("5bac973268a260001ec442d1"),
"boardId" : ObjectId("5c26ed9032f6bf001d5ce14b"),
"gradeId" : ObjectId("5bc56a319fe946001a35d5d6"),
"sectionId" : ObjectId("5959d07732018df843695f9b"),
"subjectId" : ObjectId("5c30c4b465b0850014691563"),
"topicId" : ObjectId("5c3c0a5889c70900188b9c74"),
"startDate" : ISODate("2019-03-31T23:59:50.000+05:30"),
"endDate" : ISODate("2019-04-06T23:59:50.000+05:30"),
"totalSessions" : 7,
"lastUpdatedBy" : ObjectId("5a1b996690be901900bc2584"),
"lastUpdationTime" : ISODate("2019-04-05T00:31:57.907+05:30"),
"createdBy" : ObjectId("5a1b996690be901900bc2584"),
"creationTime" : ISODate("2019-04-05T00:31:57.907+05:30"),
"validFor" : 0,
"isArchived" : false
},
i have a collection in which i want to delete documents by matching 3 conditions like
boardId
gradeId
topicId
i am using this query to delete one doc from collection but this query does not deleting the documents what am i doing wrong ?

I think your matchig conditions are not right,
I was able to delete document with proper matching conditions
db.LessonPlanConfiguration.deleteOne({'boardId' : ObjectId("5c26ed9032f6bf001d5ce14b") ,'gradeId' : ObjectId("5bc56a319fe946001a35d5d6") , 'topicId' : ObjectId("5c3c0a5889c70900188b9c74")})
Or you can also ,Try using Remove
db.LessonPlanConfiguration.remove({'boardId' : ObjectId("5c26ed9032f6bf001d5ce14b") ,'gradeId' : ObjectId("5bc56a319fe946001a35d5d6") , 'topicId' : ObjectId("5c3c0a5889c70900188b9c74")})

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?

Delete comments inside array using index value using mongoose

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.

how to prevent auto deletion collection records from Mongodb

Hello all I got stuck somewhere, I am working on mongodb with node.js where my collection data deleted automatically after 1 year on certain date and I want to stop that permanently how can I do that ? I have checked the available material on google but didn't got much success please help me friends ...
I have checked the index in one of my collection and it is showing data like this . Can you please tell me its is having TTL index or not
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "firstfive.teachers"
},
{
"v" : 1,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "firstname_lastname_text",
"weights" : {
"firstName" : 1,
"lastName" : 1
},
"default_language" : "english",
"language_override" : "language",
"ns" : "firstfive.teachers",
"textIndexVersion" : 2
}
]
most likely you have TTL (time to limit) index defined on collection you're working with (https://docs.mongodb.com/v3.2/core/index-ttl/)
yu can check it by running db.your_collection.getIndexes() (it will be one with expireAfterSeconds) in mongo shell.
as any other index it can be removed - but do it carefully, apparently someone did it deliberately

Mongoose findOne by reference returns null

I have some events in the event collection and some users in the users collection.
I have a mapping of events and users in another collection. The event_id field in that collection is a reference to the event collection(ObjectId). But when I search schema using the following command I get null as a response
db.eventusers.findOne({event_id :'57988cd30e9811750324c080'})
returns null
on the other hand when i search using user field which is not a reference, just a string containing user id, I get the result as follows.
db.eventusers.findOne({user_id :"578cdcdd56eaec041b6caf3e"})
{
"_id" : ObjectId("57988d190e9811750324c081"),
"created_at" : "1469615385595",
"updated_at" : "1469615618502",
"user_id" : "578cdcdd56eaec041b6caf3e",
"event_id" : ObjectId("57988cd30e9811750324c080"),
"deleted" : false,
"invited" : false,
"host" : true,
"status" : -1,
"__v" : 0
}
I have found out the solution. If the field was defined in Mongoose Schema as Schema.Types.ObjectId then in the Query id should be ObjectId rather than String. So instead of query {event_id :'57988cd30e9811750324c080'} it should be {event_id :ObjectId('57988cd30e9811750324c080')}:
db.eventusers.findOne({event_id :ObjectId('57988cd30e9811750324c080')})
{
"_id" : ObjectId("57988d190e9811750324c081"),
"created_at" : "1469615385595",
"updated_at" : "1469615618502",
"user_id" : "578cdcdd56eaec041b6caf3e",
"event_id" : ObjectId("57988cd30e9811750324c080"),
"deleted" : false,
"invited" : false,
"host" : true,
"status" : -1,
"__v" : 0
}

MongoDB - duplicate records in fs.files table or its OKAY ?..GridFS

My question is When I save file to MongoDB via GridFS in the fs.files table I have 2 similar records in it ..but the diference is only in _id and length parameter ..So I don´t know if it is OK or it problem somewhere in my code where I trying to save files ? ..
fs.chunks table got the same problem but there I couldn´t see any equal values ..
I could show you my select from fs.files and fs. chunks
EDIT :
Okay...so now I delete all the documents included in table ..and make new upload with file just ONCE..and look at the output from mongo
and the text mode output from robonongo looks like :
/* 1 */
{
"_id" : ObjectId("57144cdb5235ee50191257be"),
"filename" : "fs.files.jpg",
"contentType" : "image/jpeg",
"length" : 32520,
"chunkSize" : 261120,
"uploadDate" : ISODate("2016-04-18T02:56:27.714Z"),
"aliases" : null,
"metadata" : {
"slovo" : "auto",
"slovnyDruh" : "word"
},
"md5" : "f414e665528d8c6f66381d9fd8ce4abe"
}
/* 2 */
{
"_id" : ObjectId("57144cdb5235ee50191257bf"),
"filename" : "fs.files.jpg",
"contentType" : "image/jpeg",
"length" : 114863,
"chunkSize" : 261120,
"uploadDate" : ISODate("2016-04-18T02:56:27.659Z"),
"aliases" : null,
"metadata" : {
"slovo" : "auto",
"slovnyDruh" : "word"
},
"md5" : "f604c165cce422f3bec79333dc0b3805"
}

Resources