javascriptnode.jsmongodbnode-mongodb-native

How to list all indexes of Mongo Collection with MongoDB Node native?


Well I wanna know how I can fetch all indexes from a specific collection of mongodb. I trying with listIndexes, indexes and indexInformation, but these methods only give me empty values(array and object), but if I execute db.getCollection('truck').getIndexes() on mongo terminal, this give me all indexes.

I think maybe this is a bug, but I don't find any information about this, so let me show my examples and a screenshot from "Robo 3T".

await connection.collection('truck').indexes() // returns []
await connection.collection('truck').listIndexes().toArray() // returns []
await connection.collection('truck').indexInformation() // returns {}

enter image description here

So... Whats happend here? why these methods not working well?

Thanks :D

P.S: I'm using mongodb version 3.5.5: https://github.com/mongodb/node-mongodb-native


Solution

  • Well, after try some solutions like move language from javascript to Python and using Pymongo... and getting the same result... I decided using command interface but usin the native api in driver... Yep, I using command, let me show how:

    import {MongoClient} from 'mongodb'
    const client = await MongoClient.connect("...")
    const db = client.db("MyOwnDatabase")
    
    // the next line retrieve empty array
    db.collection('truck').indexes();
    
    // the next line retrieve all indexes 👍
    db.command({listIndexes: "truck"}).cursor.firstBatch;
    

    In Python is too similar:

    import pymongo
    client = pymongo.MongoClient("...")
    db = client.MyOwnDatabase
    
    # retrieve empty object
    db.truck.index_information()
    
    # retrieve all indexes 👍
    db.command("listIndexes", "truck") 
    

    I think this problem is about the driver... both are official driver, but none works well :D

    P.S: I know this question is about javascript, but I found the same problem in Python and this is the solution for both.