简体   繁体   English

如果_id在MongoDB中有什么特殊用途吗?

[英]If _id has any special usage in MongoDB?

I have one nodejs app, which define answer like this:我有一个 nodejs 应用程序,它定义了这样的答案:

var answerSchema = mongoose.Schema({
    session  : String, // the ID of the session
    question : String, // the ID of the question
    answer   : Number  // the selected answer (1-5)
});

stored as answer存储为答案

var sessionSchema = mongoose.Schema({
    name   : String, // the name of the session, like 'SAM' or 'SA'
    currQ  : Number, // the current question being answered
    state  : String, // the state of the current question: new, done, asking, summarize
    active : Number  // 1 if active, 0 otherwise
});

stored as session.存储为会话。

In the ejs file, one function get one answer-tpye variable and use在 ejs 文件中,一个函数得到一个 answer-tpye 变量并使用

var reply = JSON.parse(res);

then然后

session = reply.session._id;

I am really confused what's this line means.我真的很困惑这条线是什么意思。 There are still many case when _id is used but they didn't define it.使用 _id 的情况仍然很多,但他们没有定义它。

_id is added to all MongoDB documents by default.默认情况下,_id 被添加到所有 MongoDB 文档中。

Mongoose also adds an id getter for a string representation of _id so you can also call Mongoose 还为 _id 的字符串表示添加了一个 id getter,因此您也可以调用

reply.session.id

These properties will be available without being explicitly added to the schema这些属性无需显式添加到架构中即可使用

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

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