简体   繁体   English

如何通过Restheart API在Mongo DB上插入数据

[英]how to insert data on mongo db through restheart api

i am using mongo DB 3.0 version and restheart API 2.0 version. 我正在使用mongo DB 3.0版本和restheart API 2.0版本。 now i am trying to check my queries (URI) through postman chrome interface. 现在,我正在尝试通过邮递员Chrome界面检查我的查询(URI)。 and i create a database(test) table(mycol) and two documents in mongo DB, when i filter that data it shows correctly but when i try to insert data into mongodb through postman or HAL Browser it shows error, can you peoples please guide me the syntax format. 我在mongo DB中创建了一个数据库(测试)表(mycol)和两个文档,当我过滤该数据时,它可以正确显示,但是当我尝试通过邮递员或HAL浏览器将数据插入到mongodb中时,它显示错误,请大家指导我的语法格式。

Query for filter data, it gives correct result 查询过滤器数据,它给出正确的结果

Query for insert a document, it shows some error 查询插入文档,它显示一些错误

and also i need to know <docid> in the URI format : /<dbname>/<collname>/<docid>[?doc_type=TYPE] what it means <docid> please explain in detail with some example 并且我还需要知道URI格式的<docid>:/ <dbname> / <collname> / <docid> [?doc_type = TYPE]表示<docid>的含义,请用一些示例详细解释

To create a document you need either to POST the collection POST /test/mycol or PUT a document PUT /test/mycol/<docid> 要创建文档,您需要POST集合POST /test/mycol或PUT一个文档PUT /test/mycol/<docid>

<docid> stands for document id. <docid>代表文档ID。 the query parameter docid_type is optional and allows to specify the type of the <docid> in the URL, more information in the documentation Resource URI section. 查询参数docid_type是可选的,并允许在URL中指定<docid>的类型,有关更多信息,请参见文档的“ 资源URI”部分。

For instance, if you want to create the following document { "_id": “mydoc", “message”: “hello” } you do 例如,如果您要创建以下文档{ "_id": “mydoc", “message”: “hello” }可以执行

PUT /test/mycol/mydoc { “message”: “hello”}

or 要么

POST /test/mycol { "_id": “mydoc", “message”: “hello” }

In the latter case, if you don't specify the _id, it will be autogenerated as an ObjectId. 在后一种情况下,如果不指定_id,它将自动生成为ObjectId。

Note that you have to specify the Content-Type request header to be either application/json or application/hal+json . 请注意,您必须将Content-Type请求标头指定为application/jsonapplication/hal+json

For instance, using Postman you set the body to be raw and select JSON (application/json) from the dropdown on the right. 例如,使用Postman将正文设置为原始,然后从右侧的下拉列表中选择JSON(application / json) You'll notice that this will add the Content-Type header to the headers. 您会注意到,这会将Content-Type标头添加到标头中。

restheart-与邮递员邮寄

I run into the same problem. 我遇到了同样的问题。 The problem was I used field names starting with "_" ie field names like "_type", "_name". 问题是我使用以“ _”开头的字段名称,即“ _type”,“ _ name”之类的字段名称。 Try to avoid such names. 尽量避免使用此类名称。

I had the same problems as you. 我和你有同样的问题。 For insert you should just write your object like this: 对于插入,您应该像这样编写对象:

   {
    "code": 20,
    "name": "s",
    "family": "x"
   }

And set POST your method, also for update if the document exists it will be updated, otherwise it will be created. 并设置POST您的方法,如果文档存在,也要进行更新,否则它将被更新,否则将被创建。

Please look at this link for more information https://community.boomi.com/s/article/howtointegratewithmongodbusingopensourcerestheartlibrary#jive_content_id_Scenario_1__InsertUpdate_an_Employee_record_in_Employees_collection_using_POST 请查看此链接以获取更多信息https://community.boomi.com/s/article/howtointegratewithmongodbusingopensourcerestheartlibrary#jive_content_id_Scenario_1__InsertUpdate_an_Employee_record_in_Employees_collection_using_POST

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

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