简体   繁体   English

如何使用 NodeJS MongoDB 获取完整的索引信息以便能够重新创建相同的索引

[英]How to get full index information using NodeJS MongoDB to be able to recreate the same index

Docs say, use Collection.indexInformation({full: true}) to return full information about the indexes on collection.文档说,使用 Collection.indexInformation({full: true}) 返回有关集合索引的完整信息。 However, this only gives partial info.但是,这仅提供部分信息。

https://mongodb.github.io/node-mongodb-native/3.5/api/Collection.html#indexInformation https://mongodb.github.io/node-mongodb-native/3.5/api/Collection.html#indexInformation

I need full info so that I can "copy" (recreate) the same index on a different collection.我需要完整的信息,以便我可以在不同的集合上“复制”(重新创建)相同的索引。

What am I doing wrong?我究竟做错了什么? Thanks!谢谢!

The response does not separate the options and keys of index, rather it merges them into one object.响应不会将索引的optionskeys分开,而是将它们合并为一个对象。 To be able to recreate the same index, you need to parse it.为了能够重新创建相同的索引,您需要解析它。

export function parseIndexInformation(index) {
  const keys = index.key;
  const options = {};

  delete index.v;
  delete index.ns;
  delete index.key;

  for (let option in index) {
      options[option] = index[option];
  }

  return {keys, options};
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM