Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.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
Javascript mongodb中的字段自动递增_Javascript_Python_Mongodb - Fatal编程技术网

Javascript mongodb中的字段自动递增

Javascript mongodb中的字段自动递增,javascript,python,mongodb,Javascript,Python,Mongodb,每当我向集合中的嵌套数组插入新项时,我需要找到一种方法将“Bill_id”字段自动递增1: 我还在官方文档中找到了这个演示: 但是我不知道如何使用这个解决方案,因为它提出了一个JavaScript函数来完成所有的工作 不过,我正在使用python脚本,具体来说,是使用flask编写RESTAPI,用python编写文档中提到的类似函数。这是我用的 def getLastUserId(self): if len(list(self.find())) is not 0: last_us

每当我向集合中的嵌套数组插入新项时,我需要找到一种方法将“Bill_id”字段自动递增1: 我还在官方文档中找到了这个演示:

但是我不知道如何使用这个解决方案,因为它提出了一个JavaScript函数来完成所有的工作
不过,我正在使用python脚本,具体来说,是使用flask编写RESTAPI,用python编写文档中提到的类似函数。这是我用的

def getLastUserId(self):
  if len(list(self.find())) is not 0:
    last_user = list(self.find({}).sort("user_id", -1).limit(1))
    return last_user[0]["user_id"]
  else:
    return 0
这将返回最后添加的文档的“用户id”。只需将其增加1,并对新文档进行简单的插入

def getLastUserId(self):
  if len(list(self.find())) is not 0:
    last_user = list(self.find({}).sort("user_id", -1).limit(1))
    return last_user[0]["user_id"]
  else:
    return 0