5

I have a collection 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 collection TextDocuments which has name = doc2 when Am running following query in mongo shell

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

But in second scenario I have 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 executing this from mongo shell using below command

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

documents are not getting removed. Any solution?

Rohit Kumar
  • 1,018
  • 3
  • 12
  • 23
  • 2
    Not knowing the actual format of your documents, are you certain that the query you are generating is valid? – Lix May 07 '14 at 11:19
  • mycollection have name property and i am trying to remove collection's document on basis of name property. This is working if am doing it in mongo shell but not working with shell script. – Rohit Kumar May 07 '14 at 11:21
  • 2
    @RohitKumar Yes.. I can see that from your code. What I'm asking is whether your query actually returns results... Can you run these commands manually and see the documents being removed? – Lix May 07 '14 at 11:22
  • @Lix when am running db['mycollection'].remove({"name":"name1"}) manually it is removing document from database. – Rohit Kumar May 07 '14 at 11:25
  • In short, show us the documents containing `{ "name": "name1" }` etc, that are causing the problem. You are doing something wrong because no-one else can replicate this. Have a good look and edit your question. – Neil Lunn May 07 '14 at 11:26
  • @RohitKumar - Perhaps you can try initializing the mongo shell with the relevant database instead of having that inside the script? – Lix May 07 '14 at 11:39
  • @NeilLunn I have explained whole scenario. Any solution? – Rohit Kumar May 07 '14 at 11:54
  • 3
    One other tip is that you can just do `mongo collectionRemove.js` with the script you're using. No need for the `--eval`. Also, why not add some debugging to the script, like before the remove: `print(db['TextDocuments'].find({name:names[i]}).length())` to show the # of matches found before the removal? – WiredPrairie May 07 '14 at 12:33

1 Answers1

0

What you show is not self-consistent.

If you actually had the two documents you listed, then your remove command would remove 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 let's try your JS file contents (after re-inserting those same two documents, of course):

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

Again, two documents are removed, by the second pass through the loop (since you don't actually have "doc1" name value in the small example you gave.

Let's now insert one doc with "doc1" and one with "doc2" and try a slightly more revealing version of your 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 }
Asya Kamsky
  • 41,784
  • 5
  • 109
  • 133