【问题标题】:MongoDB with Ruby driver how to create fields with curl?带有 Ruby 驱动程序的 MongoDB 如何使用 curl 创建字段?
【发布时间】:2015-11-10 18:42:04
【问题描述】:

我正在向我的 Ruby 驱动程序发送以下 curl 请求

curl -i -X POST -H "Content-Type:application/json" -d '{  "firstName" : "Frodo",  "lastName" : "Baggins" }' http://localhost:4567/new_document/?

这是 ruby​​ 中 POST 操作的代码。

post '/new_document/?' do
  content_type :json
  db = settings.mongo_db
  result = db.insert_one params
  db.find(:_id => result.inserted_id).to_a.first.to_json
end

我在控制台中得到以下响应。

HTTP/1.1 200 OK 
Content-Type: application/json
Content-Length: 43
X-Content-Type-Options: nosniff
Server: WEBrick/1.3.1 (Ruby/2.0.0/2013-11-22)
Date: Tue, 10 Nov 2015 18:38:59 GMT
Connection: Keep-Alive

{"_id":{"$oid":"564239c3e89bde194d000007"}}

正如您所见,永远不会创建名字和姓氏字段。我做错了什么?

【问题讨论】:

    标签: ruby mongodb curl terminal


    【解决方案1】:

    想通了!这将创建正确的字段及其值。

    curl -d 'name=adam&last=hoffman' http://localhost:4567/new_document/?
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-09
      • 1970-01-01
      相关资源
      最近更新 更多