skip to Main Content

I’ve accidentally named a view : "test..view".
When I try to delete it now I get the following error :
MongoInvalidArgumentError: Collection names cannot be empty

I tried running the following command :
db.getCollection("test..view").drop()

I also tried to rename it but got the same error.

2

Answers


  1. Chosen as BEST ANSWER

    You can use runCommand to solve this issue

    test> show collections
    foo
    test..view        [view]
    test> db.runCommand({drop: 'test..view'})
    { ns: 'test.test..view', ok: 1 }
    test> show collections
    foo
    

    Ultimately, the error "Collection names cannot be empty" comes from the client you're using, using Compass and the mongo node module triggered the issue for me. Using my system mongoshell didn't trigger that error.


  2. This seems to work:

    MongoDB Enterprise rs0:PRIMARY> db.createCollection("bar..baz");
    {
        "ok" : 1,
        "$clusterTime" : {
            "clusterTime" : Timestamp(1701808000, 1),
            "signature" : {
                "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                "keyId" : NumberLong(0)
            }
        },
        "operationTime" : Timestamp(1701808000, 1)
    }
    MongoDB Enterprise rs0:PRIMARY> show collections
    bar..baz
    other
    ...
    MongoDB Enterprise rs0:PRIMARY> db.getCollection("bar..baz").insert({x:1});
    WriteResult({ "nInserted" : 1 })
    MongoDB Enterprise rs0:PRIMARY> db.getCollection("bar..baz").find();
    { "_id" : ObjectId("656f878cc0f674eaa9d2c6d0"), "x" : 1 }
    MongoDB Enterprise rs0:PRIMARY> db.getCollection("bar..baz").drop();
    true
    MongoDB Enterprise rs0:PRIMARY> db.getCollection("bar..baz").find();
    MongoDB Enterprise rs0:PRIMARY> 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search