Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 获取匹配值的索引_Mongodb_Mongodb Stitch - Fatal编程技术网

Mongodb 获取匹配值的索引

Mongodb 获取匹配值的索引,mongodb,mongodb-stitch,Mongodb,Mongodb Stitch,我在mongodb atlas中创建了在线集群,然后创建了mongodb stitch应用程序,在stitch javascript编辑器中,如何在匹配x时获得索引?例如: { "_id": 5646546, "items":[ {"x": 12, "y": 4} , {"x": 12, "y": 4} ] } 因此,当x=12时,索引应为0,您可以用stitch编写javascript函数,如下所示: const getIndex = (inputArray, match) =&

我在mongodb atlas中创建了在线集群,然后创建了mongodb stitch应用程序,在stitch javascript编辑器中,如何在匹配x时获得索引?例如:

{
"_id": 5646546,
"items":[   {"x": 12, "y": 4} ,  {"x": 12, "y": 4}   ]

}

因此,当x=12时,索引应为0,您可以用stitch编写javascript函数,如下所示:

const getIndex = (inputArray, match) => {
  return inputArray.findIndex(element => element.x == match);
};

let obj = {
  _id: 5646546,
  items: [
    { x: 12, y: 4 },
    { x: 12, y: 4 }
  ]
};

let obj2 = {
  _id: 5646547,
  items: [
    { x: 121, y: 4 },
    { x: 12, y: 4 },
    { x: 122, y: 4 }
  ]
};

console.log(getIndex(obj.items, 12)); // prints 0
console.log(getIndex(obj2.items, 122)); // prints 2

您可以在stitch中编写javascript函数,如下所示:

const getIndex = (inputArray, match) => {
  return inputArray.findIndex(element => element.x == match);
};

let obj = {
  _id: 5646546,
  items: [
    { x: 12, y: 4 },
    { x: 12, y: 4 }
  ]
};

let obj2 = {
  _id: 5646547,
  items: [
    { x: 121, y: 4 },
    { x: 12, y: 4 },
    { x: 122, y: 4 }
  ]
};

console.log(getIndex(obj.items, 12)); // prints 0
console.log(getIndex(obj2.items, 122)); // prints 2

所以你想写一个javascript函数?是的,我使用python,所以我对javascript有一点了解来解释更多,我使用stitch将收藏导入goole sheet,一切正常。我想知道的是,google sheet中的更改同步到我的数据库,因此我将制作google应用程序脚本,返回id和changes>,在stitch一侧,我将搜索id,即(x)在我的示例和更新ySo中,您想编写一个javascript函数吗?是的,我使用python,所以我对JavaScription了解一点,以便进一步解释,我使用stitch将收藏导入goole sheet,一切正常我想知道的是google sheet中的更改同步到我的数据库,所以我将制作google应用程序脚本,返回id和changes>,在stitch一侧,我将搜索id,这是我示例中的(x),并更新Ytank you以获得解决方案,但我得到了erorr@ManafA.B:我想问题出在第17行,去掉它,你们还需要记录响应吗?你也可以删除评论打印。谢谢你的解决方案,但我得到了erorr@ManafA。B:我想问题在第17行,删除它,你还需要记录回复吗?您也可以删除注释打印。。