简体   繁体   English

如何使用Monk检索MongoDb集合ID

[英]How to Retrieve MongoDb collection Id with Monk

I'm trying to do a very simple app 'todo app' on koajs with swig and I have succeeded in inserting data to Mongodb, in my case the name of the db is 'mytodos' and the collection is 'todos'. 我正在尝试使用swig在koajs上做一个非常简单的应用程序“ todo app”,并且我已经成功地将数据插入到Mongodb中,在我的情况下,数据库的名称是“ mytodos”,集合是“ todos”。

How can I retrieve the '_id' of a document? 如何检索文档的“ _id”? I tried doing this: 我尝试这样做:

index.html (this lists the todo items) index.html(列出待办事项)

<div class="todoListClass">
  <p>
    <strong>Your todo list:</strong> {{todos.length}}
    {% for todo in todos%}
    <li>{{todo.text1}} <span>
    <a href="/todo/delete/{{todo._id}}">Delete</a></span></li>
    {%endfor%}
  </p>
</div>

but I can't seem to get the result that I want. 但我似乎无法获得想要的结果。 It just returns '[Object][object]'. 它只返回'[Object] [object]'。

The _id field of your todo item is loaded from mongodb for displaying the _id field which is of type ObjectId you will need to get a string representation by calling _id.toString() . 您的待办事项的_id字段是从mongodb加载的,用于显示类型为ObjectId的_id字段,您需要通过调用_id.toString()获得字符串表示形式。

<div class="todoListClass">
  <p>
    <strong>Your todo list:</strong> {{todos.length}}
    {% for todo in todos%}
    <li>{{todo.text1}} <span>
    <a href="/todo/delete/{{todo._id.toString()}}">Delete</a></span></li>
    {%endfor%}
  </p>
</div>

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

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