Db.collection.remove () does not delete the document

I have a collection of TextDocuments

/* 0 */
{
 name:"doc2",
 pages:[{
 pageNumber:"1",
 text:"This is first page text",


 },{
 pageNumber:"2",
 text:"This is second page text",


 },{
 pageNumber:"3",
 text:"This is third page text",


 }]
 }
 /* 1 */
 {
 name:"doc2",
 pages:[{
 pageNumber:"1",
 text:"This is first page text",


 },{
 pageNumber:"2",
 text:"This is second page text",


 },{
 pageNumber:"3",
 text:"This is third page text",


 }]
 }

I want to remove documents from the TextDocuments collection named = doc2 when Am runs the following request in the mongo shell

rohitkumar@ubuntuhost:~$ mongo
> use mydb
switched to db mydb
> db.TextDocuments.remove({"name":"doc2"})
WriteResult({ "nRemoved" : 1 })
>

But in the second script, I created a shell script

//File name collectionRemove.js
    var db =  connect("localhost:27017/mydb");
    var names= ["doc1","doc2"];


    for(i=0;i<names.length;i++){

    db['TextDocuments'].remove({"name":names[i]});

    }

when doing this from mongo shell using command

rohitkumar@ubuntuhost:~$mongo mydb  --eval "load('collectionRemove.js')"

Documents are not deleted. Any solution?

+4
source share
1 answer

What you show is not self-consistent.

If you really have two documents that you specified, your remove command will delete two documents, not one.

db.TextDocuments.insert({ name:"doc2",  pages:[{  pageNumber:"1",  text:"This is first page text", },{  pageNumber:"2",  text:"This is second page text", },{  pageNumber:"3",  text:"This is third page text", }]})
WriteResult({ "nInserted" : 1 })
db.TextDocuments.insert({ name:"doc2",  pages:[{  pageNumber:"1",  text:"This is first page text", },{  pageNumber:"2",  text:"This is second page text", },{  pageNumber:"3",  text:"This is third page text", }]})
WriteResult({ "nInserted" : 1 })
db.TextDocuments.remove({"name":"doc2"})
WriteResult({ "nRemoved" : 2 })

Now try the contents of your JS file (after reinstalling the same two documents, of course):

var db = connect ( "localhost: 27017/so" ); : localhost: 27017/so var names = [ "doc1" , "doc2" ]; ( = 0;

, , ( , , "doc1" .

"doc1" "doc2" script:

db.TextDocuments.insert({ name:"doc1",  pages:[{  pageNumber:"1",  text:"This is first page text", },{  pageNumber:"2",  text:"This is second page text", },{  pageNumber:"3",  text:"This is third page text", }]})
WriteResult({ "nInserted" : 1 })
db.TextDocuments.insert({ name:"doc2",  pages:[{  pageNumber:"1",  text:"This is first page text", },{  pageNumber:"2",  text:"This is second page text", },{  pageNumber:"3",  text:"This is third page text", }]})
WriteResult({ "nInserted" : 1 })
var db =  connect("localhost:27017/so");
connecting to: localhost:27017/so
for(i=0;i<names.length;i++){    print("Removing name " + names[i]); printjson(db['TextDocuments'].remove({"name":names[i]})); }
Removing name doc1
{ "nRemoved" : 1 }
Removing name doc2
{ "nRemoved" : 1 }
0

Source: https://habr.com/ru/post/1539562/


All Articles