如何找到只有一个文档Restivus和Curl [英] How to find just one document with Restivus and Curl
问题描述
我有一个简单的初学者使用Restivus的问题。
I have a simple beginners problem with using Restivus.
在我的app.js中我已经声明:
In my app.js I have declared:
Players = new Mongo.Collection("players");
及更高版本:
if (Meteor.isServer) {
// Global configuration
Api = new Restivus({
useDefaultAuth: true,
prettyJson: true
});
// Generates: GET/POST on /api/v1/players, and GET/PUT/DELETE on /api/v1/players/:id
// for Meteor.users collection (works on any Mongo collection)
Api.addCollection(Players);
// That's it! Many more options are available if needed...
}
然后我开始meteor mongo
I then start "meteor mongo"
meteor:PRIMARY> db.players.insert({username:"Kalle", location:"Home"})
WriteResult({ "nInserted" : 1 })
meteor:PRIMARY> db.players.insert({username:"Pelle", location:"Away"})
WriteResult({ "nInserted" : 1 })
meteor:PRIMARY> db.players.find({});
{ "_id" : ObjectId("56598d83846a7b53e8a1176b"), "username" : "Kalle","location" : "Home" }
{ "_id" : ObjectId("56598d8c846a7b53e8a1176c"), "username" : "Pelle", "location" : "Away" }
我得到:
curl -X GET http://localhost:3000/api/players
{
"status": "success",
"data": [
{
"_id": {
"_str": "56598d83846a7b53e8a1176b"
},
"username": "Kalle",
"location": "Home"
},
{
"_id": {
"_str": "56598d8c846a7b53e8a1176c"
},
"username": "Pelle",
"location": "Away"
}
]
但是当我尝试找到一个特定文档时:
BUT when I then try to find one particular document:
curl -X GET http://localhost:3000/api/players/56598d83846a7b53e8a1176b
{
"status": "fail",
"message": "Item not found"
}
为什么找不到文档和处理什么
Why doesn't it find the document and whats the deal with the:
"_id": {
"_str": "56598d83846a7b53e8a1176b"
}
推荐答案
meteor mongo
简单地打开一个MongoDB shell,这些文档是使用 _id
(这是MongoDB id的默认类型)的ObjectId创建的。
Since meteor mongo
simply opens a MongoDB shell, the documents were created with an ObjectId for an _id
(which is the default type for MongoDB ids).
Meteor默认为 _id
的随机字符串。因此,您的示例中的 _id
不仅仅是您看到的字符串,而是整个ObjectId包含它。
Meteor defaults to random strings for _id
s. Therefore, the _id
in your example is not simply the string that you see, but the entire ObjectId containing it.
为了使它工作,在mongo shell中显式地设置 _id
或者(可能更好)使用 meteor shell
请直接从Meteor服务器插入文档。
To make it work, either explicitly set an _id
in the mongo shell or (probably better) use meteor shell
to insert the documents directly from the Meteor server.
这篇关于如何找到只有一个文档Restivus和Curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!