Node + Mongoose:获取最后插入的ID? [英] Node + Mongoose: Get last inserted ID?

查看:262
本文介绍了Node + Mongoose:获取最后插入的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索最后插入的 _id ,使用mongoose作为node.js的MongoDB包装器。我发现以下教程,但是我不能更改任何节点模块,因为应用程序在公共服务器上运行:

I want to retrieve the last inserted _id, using mongoose as MongoDB wrapper for node.js. I've found the following tutorial, but I can't change any node modules because the app runs on a public server:

获取最后插入的ID(提示 - 您必须攻击Mongoose)

其他任何想法?这是我想做的:

Any other ideas? This what I want to do:


  • 插入新用户

  • 获取用户的 _id value

  • 根据用户ID设置新会话

  • 重定向到/

  • Insert new user
  • Get user's _id value
  • Set a new session based on user's id
  • Redirect to /

谢谢!

推荐答案

我正在使用mongoose版本1.2 .0,一旦我创建了一个新的mongoose模型实例,就已经设置了 _id

I'm using mongoose version 1.2.0 and as soon as I created a new instance of a mongoose model, the _id is already set.

coffee> u = new User()
[object Object]
coffee> u._id
4dd68fc449aaedd177000001

我也验证了在我打电话 u.save() _id 保持不变。我通过MongoHub验证,这确实是保存到MongoDB中的真实ID。

I also verified that after I call u.save() the _id remains the same. I verified via MongoHub that this is indeed the real ID saved into MongoDB.

这篇关于Node + Mongoose:获取最后插入的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆