如何从CouchDb获得独特的价值?

时间:2022-11-30 12:42:24

In couchdb i have a array field:

在couchdb我有一个数组字段:

numbers:["79998887766","79998887755","79998887766","79998887744"]

In node.js app i want to have only unique numbers.Like this:

在node.js应用程序中,我想只有唯一的数字。就像这样:

["79998887766","79998887755","79998887744"]

Its possible to do with couchdb view or node.js module cradle?
Or only way get all numbers and filter array with node?

它可以用于couchdb视图或node.js模块底座吗?或者只能通过节点获取所有数字和过滤器数组?

3 个解决方案

#1


1  

it's fairly straightforward, if inelegant, to iterate across the array twice testing for duplicates. since this is O(n²) it won't scale well for big arrays; i would look for a more sophisticated solution if you expect your numbers array to contain hundreds or more items

它是相当简单的,如果不优雅,迭代数组两次测试重复。因为这是O(n²),所以对于大型阵列来说它不能很好地扩展;如果您希望数字数组包含数百个或更多项,我会寻找更复杂的解决方案

function testExists(item, arr2){
  var exists = false;
  arr2.forEach(function(item2){
    if(item === item2){
      exists = true;
    } 
  });
  return exists;
};

function removeDupes(arr){
  var output = [];
  arr.forEach(function(item){
    var exists = testExists(item, output);
    if(!exists){
      output.push(item);
    };
  });
  return output;
};

console.log(removeDupes(["a","b","a","c"]));

#2


1  

I believe there is no native command for implementing this in CouchDB. A possible solution for the problem would be to make use of something like redis sets. There are also a few third party libraries that implement sets in Javascript like this one : https://github.com/PeterScott/simplesets-nodejs.

我相信在CouchDB中没有用于实现它的本机命令。问题的一个可能的解决方案是使用像redis集这样的东西。还有一些第三方库在Javascript中实现集合,如下所示:https://github.com/PeterScott/simplesets-nodejs。

#3


1  

You can do either in CouchDB view or in node.js code after fetching data. In both cases you'll need to write javascript code to exclude duplicates. You can use objects of this:

您可以在获取数据后在CouchDB视图或node.js代码中执行操作。在这两种情况下,您都需要编写javascript代码以排除重复项。你可以使用这个对象:

var obj = {};

numbers.forEach(function (num) {
    obj[num] = true;
});

var unique = Object.keys(obj);

It works if order of keys doesn't matter. Doing this in a view is a better way, since the results of computation will be reused.

如果键的顺序无关紧要,它就可以工作。在视图中执行此操作是一种更好的方法,因为计算结果将被重用。

#1


1  

it's fairly straightforward, if inelegant, to iterate across the array twice testing for duplicates. since this is O(n²) it won't scale well for big arrays; i would look for a more sophisticated solution if you expect your numbers array to contain hundreds or more items

它是相当简单的,如果不优雅,迭代数组两次测试重复。因为这是O(n²),所以对于大型阵列来说它不能很好地扩展;如果您希望数字数组包含数百个或更多项,我会寻找更复杂的解决方案

function testExists(item, arr2){
  var exists = false;
  arr2.forEach(function(item2){
    if(item === item2){
      exists = true;
    } 
  });
  return exists;
};

function removeDupes(arr){
  var output = [];
  arr.forEach(function(item){
    var exists = testExists(item, output);
    if(!exists){
      output.push(item);
    };
  });
  return output;
};

console.log(removeDupes(["a","b","a","c"]));

#2


1  

I believe there is no native command for implementing this in CouchDB. A possible solution for the problem would be to make use of something like redis sets. There are also a few third party libraries that implement sets in Javascript like this one : https://github.com/PeterScott/simplesets-nodejs.

我相信在CouchDB中没有用于实现它的本机命令。问题的一个可能的解决方案是使用像redis集这样的东西。还有一些第三方库在Javascript中实现集合,如下所示:https://github.com/PeterScott/simplesets-nodejs。

#3


1  

You can do either in CouchDB view or in node.js code after fetching data. In both cases you'll need to write javascript code to exclude duplicates. You can use objects of this:

您可以在获取数据后在CouchDB视图或node.js代码中执行操作。在这两种情况下,您都需要编写javascript代码以排除重复项。你可以使用这个对象:

var obj = {};

numbers.forEach(function (num) {
    obj[num] = true;
});

var unique = Object.keys(obj);

It works if order of keys doesn't matter. Doing this in a view is a better way, since the results of computation will be reused.

如果键的顺序无关紧要,它就可以工作。在视图中执行此操作是一种更好的方法,因为计算结果将被重用。