使用Ruby检索唯一记录将MongoDB _id转换为ObjectID对象(Turn MongoDB _id into an ObjectID object using Ruby to Retrieve Unique Record)
我正在尝试使用标准类型ObjectID的_id检索存储在MongoDB中的文档。 我将_id表示为字符串。 在这个例子中它是“4ec064e406a6f1205a000001”
所以我这样做:
require 'mongo' connection = Mongo::Connection.new("localhost", 27017).db("store") collection = connection.collection("products") id = '4ec064e406a6f1205a000001' # What should the following line be? This doesn't work. collection.find_one("_id" => Mongo::ObjectID.from_string(id))
我意识到这是一个BSON ObjectID,我尝试了一些不同的方法来使用bson gem生成正确的对象,但我无法让它工作。 我也尝试使用类似的代码将对象的_id中的一个更改为标准的Int32,并且它工作正常。 我不知道如何创建用于此查询的正确ObjectID对象。
谢谢!
I'm trying to retrieve a document stored in MongoDB using its _id of the standard type ObjectID. I have the _id represented as a string. In this example it's "4ec064e406a6f1205a000001"
So I do:
require 'mongo' connection = Mongo::Connection.new("localhost", 27017).db("store") collection = connection.collection("products") id = '4ec064e406a6f1205a000001' # What should the following line be? This doesn't work. collection.find_one("_id" => Mongo::ObjectID.from_string(id))
I realize this is a BSON ObjectID and I tried some different ways of using the bson gem to generate the right object but I can't get it to work. I also tried to change one of the object's _id to a standard Int32 using similar code and it worked fine. I don't know how to create the right ObjectID object to use with this query.
Thanks!
原文:https://stackoverflow.com/questions/8116414
最满意答案
我从未见过这种用法,但有类似的东西:
var query = string.Join("&",( from parent in XElement.Parse(xml).Elements() from child in parent.Elements() select HttpUtility.UrlEncode(parent.Name.LocalName) + "_" + HttpUtility.UrlEncode(child.Name.LocalName) + "=" + HttpUtility.UrlEncode(child.Value)).ToArray());
I've never seen that usage, but something like:
var query = string.Join("&",( from parent in XElement.Parse(xml).Elements() from child in parent.Elements() select HttpUtility.UrlEncode(parent.Name.LocalName) + "_" + HttpUtility.UrlEncode(child.Name.LocalName) + "=" + HttpUtility.UrlEncode(child.Value)).ToArray());
相关问答
更多-
C#发布XML到URL不起作用(C# Post XML to URL not working)[2023-06-25]
是的,使用System.Net进行表单提交有一个更简单的方法... WebClient类是你的朋友。 在这种情况下,您可以使用webClient :: UploadValues(),它会处理所有其他事情。 Yes, there is an easier way to do a Forms submit using System.Net... The WebClient class is your friend. In this case you would use webClient::UploadValu ... -
如何防止在C#TBB中的xml文档中编码字符<>?(How can I prevent encoding of the characters < > in an xml document in my C# TBB?)[2019-12-01]
您可以使用另一个TBB将所有内容解码回来,您应将其作为模板中最后一个TBB放置。 另请注意,如果您的模板中有Dreamweaver TBB,它将创建包含所有内容的“输出”项(与Nuno的答案内联,使ContentType为HTML)。 只需将其解码即可。 不要担心对不需要编码的字符(例如百分号)进行编码,因为它们将被双重编码,当您转换回来时,您将完全得到这些字符。 这是您可以使用的C#Fragment TBB: <%@ Import Namespace="System.Web"%> Item output ... -
HTTP POST Web请求的哪些部分需要进行URL编码?(What parts of a HTTP POST web request need to be URL Encoded?)[2023-09-11]
从application / x-www-form-urlencoded或multipart / form-data? 和HTML文档中的表单 : 控制名称和值将被转义 所以,选项2。 From application/x-www-form-urlencoded or multipart/form-data? and Forms in HTML documents: Control names and values are escaped So, option 2. -
将XML文档转换为HTTP编码的URL编码查询字符串 - C#(Converting XML document to URL encoded query string for HTTP POST - C#)[2023-10-25]
我从未见过这种用法,但有类似的东西: var query = string.Join("&",( from parent in XElement.Parse(xml).Elements() from child in parent.Elements() select HttpUtility.UrlEncode(parent.Name.LocalName) + "_" + HttpUtility.UrlE ... -
查看System.Convert.FromBase64String(XmlDoc)生成的字节很有帮助。 我已经这样做了,看看原始字符串中的“metálicas”这个词(这只是我发现的第一个带有重音的词)。 该部分字符串被转换为字节数组6D 65 74 E1 6C 69 63 61 73 。 从该字节数组中可以很容易地看到两件事: 这是单字节编码 它不是UTF-8:在UTF-8中,大于7F的字节本身不会出现,总是以2-4的组为单位。 从那里我猜测它将是某种形式的扩展ASCII, Windows-1252似乎工 ...
-
我想我可能已经弄明白了。 分解查询字符串@#$%^&+=/,?><``";:\|][{} =@#$%^&+=/,?><``";:\|][{}到键/值对(按出现顺序): 键@#$%^ ,值无 键+ ,值/,?><``";:\|][{} 键@#$%^ ,值无 键+ ,值/,?><``";:\|][{} 基于https://stackoverflow.com/a/1746566/3108853 ,没有关于如何处理重复密钥的标准,因此看起来亚马逊只是选择忽略或覆盖(无法用这个测试用例来判断) ,因为重复键的值是相同 ...
-
调用Uri.EscapeDataString 。 与HttpUtility不同,这也适用于客户端配置文件。 Call Uri.EscapeDataString. Unlike HttpUtility, this works on the client profile too.
-
由于该函数返回HTTP响应头,因此应检查'Transfer-Encoding'是否为'chunked'然后解码分块编码的字符串。 在伪代码中: CALL parse_http_header IF 'Transfer-Encoding' IS 'chunked' CALL decode_chunked 解析HTTP响应标题: 下面是将HTTP响应头解析为关联数组的功能。 function parse_http_header($str) { $lines = explode("\r\n", $str ...
-
在字节数组中以UTF8 XML文档编码ASCII字符串(Encoding of ASCII string in UTF8 XML document in Byte array)[2022-11-02]
不要介意如何编码XML文档进行传输。 做你想做的事情的正确方法 - 对某些非ASCII字符进行编码,使它们在未受损害的情况下幸免于下去 - 是使用XML字符引用来表示需要如此保留的字符。 例如,你的 ÐØë 使用XML字符引用表示 ÐØë 接收[conformant] XML处理器将/应该/必须将这些数字字符引用转换回它们所代表的字符。 这里有一些代码可以解决这个问题: public static string ConvertToXmlCharacterRefe ... -
我为你做了一个快速的功能,你应该为你实现这个功能,它会从你的key => value对创建参数,并将你的非原始值字符串化。 var objToParams = function(obj){ var paramString = ''; for (var key in data) { var value = obj[key]; if(obj[key] instanceof Array || obj[key] instanceof Object){ ...