如何使用 MongoDB Node native 列出 Mongo 集合的所有索引?
How to list all indexes of Mongo Collection with MongoDB Node native?
好吧,我想知道如何从 mongodb 的特定集合中获取所有索引。
我尝试使用 listIndexes
、indexes
和 indexInformation
,但这些方法只给我空值(数组和对象),但如果我在 mongo 上执行 db.getCollection('truck').getIndexes()
终端,这给了我所有索引。
我认为这可能是一个错误,但我没有找到任何相关信息,所以让我展示我的示例和来自 "Robo 3T" 的屏幕截图。
await connection.collection('truck').indexes() // returns []
await connection.collection('truck').listIndexes().toArray() // returns []
await connection.collection('truck').indexInformation() // returns {}
所以...这里发生了什么?为什么这些方法效果不佳?
谢谢 :D
P.S:我正在使用 mongodb
版本 3.5.5
:https://github.com/mongodb/node-mongodb-native
看起来在代码中 await connection.collection('truck').indexes()
您还需要指定数据库。不清楚什么是connection
.
以下脚本将打印指定数据库和集合中的索引。
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();
)();
好吧,在尝试了一些解决方案之后,例如将语言从 javascript 移动到 Python 并使用 Pymongo
... 并得到相同的结果...我决定使用命令界面但是在驱动程序中使用本机 api ...
是的,我使用 command
,让我展示如何:
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;
在Python太相似了:
import pymongo
client = pymongo.MongoClient("...")
db = client.MyOwnDatabase
# retrieve empty object
db.truck.index_information()
# retrieve all indexes
db.command("listIndexes", "truck")
我认为这个问题与驱动程序有关...都是官方驱动程序,但 none 运行良好: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.
listIndexes
是一个数据库命令,你必须这样调用它:
db.runCommand( {listIndexes: "truck"} )
或使用 shorthand
db.truck.getIndexes()
db.collection('truck').getIndexes()
方法indexInformation
在MongoDB中不存在(至少我没有找到)。 collection.indexes()
我也没找到。
...为了更漂亮的输出:
for ( var i=0; i < indxs.length; i++ ) {
console.log(indxs[i].ns + '.' + indxs[i].name,'key(s):-\n',indxs[i].key);
}
Nodejs 中的工作示例 indexes()
a) 定义
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) 呼叫
export function getIndexes(coll) { mongo[arguments.callee.name](coll, res => { console.log(res) }) }
好吧,我想知道如何从 mongodb 的特定集合中获取所有索引。
我尝试使用 listIndexes
、indexes
和 indexInformation
,但这些方法只给我空值(数组和对象),但如果我在 mongo 上执行 db.getCollection('truck').getIndexes()
终端,这给了我所有索引。
我认为这可能是一个错误,但我没有找到任何相关信息,所以让我展示我的示例和来自 "Robo 3T" 的屏幕截图。
await connection.collection('truck').indexes() // returns []
await connection.collection('truck').listIndexes().toArray() // returns []
await connection.collection('truck').indexInformation() // returns {}
所以...这里发生了什么?为什么这些方法效果不佳?
谢谢 :D
P.S:我正在使用 mongodb
版本 3.5.5
:https://github.com/mongodb/node-mongodb-native
看起来在代码中 await connection.collection('truck').indexes()
您还需要指定数据库。不清楚什么是connection
.
以下脚本将打印指定数据库和集合中的索引。
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();
)();
好吧,在尝试了一些解决方案之后,例如将语言从 javascript 移动到 Python 并使用 Pymongo
... 并得到相同的结果...我决定使用命令界面但是在驱动程序中使用本机 api ...
是的,我使用 command
,让我展示如何:
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;
在Python太相似了:
import pymongo
client = pymongo.MongoClient("...")
db = client.MyOwnDatabase
# retrieve empty object
db.truck.index_information()
# retrieve all indexes
db.command("listIndexes", "truck")
我认为这个问题与驱动程序有关...都是官方驱动程序,但 none 运行良好:D
P.S: I know this question is about
javascript
, but I found the same problem inPython
and this is the solution for both.
listIndexes
是一个数据库命令,你必须这样调用它:
db.runCommand( {listIndexes: "truck"} )
或使用 shorthand
db.truck.getIndexes()
db.collection('truck').getIndexes()
方法indexInformation
在MongoDB中不存在(至少我没有找到)。 collection.indexes()
我也没找到。
...为了更漂亮的输出:
for ( var i=0; i < indxs.length; i++ ) {
console.log(indxs[i].ns + '.' + indxs[i].name,'key(s):-\n',indxs[i].key);
}
Nodejs 中的工作示例 indexes()
a) 定义
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) 呼叫
export function getIndexes(coll) { mongo[arguments.callee.name](coll, res => { console.log(res) }) }