我需要創建一個代碼來隱藏/取消隱藏節點中的 mongo 索引。node-mongodb-native上沒有這樣的方法,盡管它們存在于mongo shell上。
第二次嘗試是運行自定義命令,但await db.command({ hideIndex: 'some_name' })以MongoServerError: no such command: 'hideIndex'.
最后,我知道在舊版本中有執行/評估方法,但看起來它們在某個時候被洗掉了。我知道它會帶來一些安全問題,允許某人運行自定義命令,但我不相信沒有人需要從節點運行自定義的東西,而驅動程式中可用的方法串列如此有限?
這就提出了一個問題:如何在節點 mongodb 驅動程式中運行自定義命令?
uj5u.com熱心網友回復:
錯誤直接說明發生了什么,沒有這樣的命令。Mongosh 只是對幾個命令的包裝器來實作您需要的行為,并且此幫助器在其他驅動程式中不存在(至少在大多數驅動程式中)。您可以檢查由 shell 助手執行的內部步驟hideIndex(通過在沒有 的情況下啟動此助手())并在節點中執行它們runCommand(在節點中它被稱為 just command)
MongoDB Enterprise replset:PRIMARY> db.coll.hideIndex
function(index) {
return this._hiddenIndex(index, true);
}
MongoDB Enterprise replset:PRIMARY> db.coll._hiddenIndex
function(index, hidden) {
assert(index, "please specify index to hide");
// Need an extra check for array because 'Array' is an 'object', but not every 'object' is an
// 'Array'.
var indexField = {};
if (typeof index == "string") {
indexField = {name: index, hidden: hidden};
} else if (typeof index == "object") {
indexField = {keyPattern: index, hidden: hidden};
} else {
throw new Error("Index must be either the index name or the index specification document");
}
var cmd = {"collMod": this._shortName, index: indexField};
var res = this._db.runCommand(cmd);
return res;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/464945.html
標籤:节点.js mongodb 节点-mongodb-本机 mongodb-nodejs-驱动程序
