3

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

prasad_
  • 12,755
  • 2
  • 24
  • 36
Olaf Erlandsen
  • 5,817
  • 9
  • 41
  • 73

5 Answers5

7

Looks like in the code await connection.collection('truck').indexes() you need to specify the database also. It is not clear what is connection.

The following script will print the indexes in the specified database and collection.

const MongoClient = require('mongodb').MongoClient;

( async function() {
     const client = new MongoClient('mongodb://localhost:27017', { useNewUrlParser: true, useUnifiedTopology: true } );
     try {
        await client.connect();
        const coll = client.db('test').collection('books');
        const indxs = await coll.indexes();
        console.log(indxs);     // prints all the indexes
     } catch (err) {
         console.log(err.stack);
     }
     client.close();
)();
prasad_
  • 12,755
  • 2
  • 24
  • 36
2

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.

Olaf Erlandsen
  • 5,817
  • 9
  • 41
  • 73
1

listIndexes is a database command, you have to call it like this:

db.runCommand( {listIndexes: "truck"} )

or using the shorthand

db.truck.getIndexes()
db.collection('truck').getIndexes()

Method indexInformation does not exist in MongoDB (at least I did not find it). collection.indexes() I did not find either.

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
1

...for prettier output:

for ( var i=0; i < indxs.length; i++ ) {
    console.log(indxs[i].ns + '.' + indxs[i].name,'key(s):-\n',indxs[i].key);
}
0

Working Example in Nodejs with indexes()

a) Definition

export function getIndexes(coll, callback: Function) {
MongoClient.connect(process.env.uri, (err, db) => {
    db.db(dbWeb).collection(coll).indexes( (err, res) => {
        callback(res)
        db.close();
    })
})

}

b) Call

export function getIndexes(coll) { mongo[arguments.callee.name](coll, res => { console.log(res) }) }
Timo
  • 2,922
  • 3
  • 29
  • 28