ashleydavis / mongodb-rest

REST Server for MongoDB (using node.js)
GNU Lesser General Public License v3.0
75 stars 34 forks source link

Insert with pre specified _id isn't working #19

Closed tinspu closed 8 years ago

tinspu commented 8 years ago

curl -d '{ "A1" : 201, "_id": ObjectId("4090e196b0c7f46870000000") }' -H "Content-Type: application/json" http://localhost:3000/test/example1

mind you the id is not there(it's unique), and when i try with out ObjectId() like this

curl -d '{ "A1" : 201, "_id": "4090e196b0c7f46870000000") }' -H "Content-Type: application/json" http://localhost:3000/test/example1

node crashs ....

ashleydavis commented 8 years ago

Unfortunately you can't use ObjectId in JSON data. It won't deserialize.

Your second example seems to work for me:

curl -d '{ "A1" : 201, "_id": "4090e196b0c7f46870000000" }' -H "Content-Type: application/json" http://localhost:3000/test/example1

I'm running node 5.3.0, what are you running?

In this case your ID will be stored in the database as a string rather than an ObjectID. For mongodb-rest to support this it would have to automatically _id fields from string to ObjectId after the JSON data has been deserialized, although I'm not sure that's a good idea.

ghost commented 8 years ago

Unfortunately ObjectID seems to be too strict and breaks with validation when you have documents like: { _id: "234", a: 1 }, or even { _id: "000000000000000000000234", a: 1 }.

curl http://127.0.0.1:3000/test/example1/234
Error: Argument passed in ...

curl http://127.0.0.1:3000/test/example1/000000000000000000000234
{
    "ok": 0
}
ashleydavis commented 8 years ago

If you find a way around it please contribute and I'll accept your pull request. Thanks.