MongoDB:Can';t规范化查询:BadValue未知运算符
数据如下:MongoDB:Can';t规范化查询:BadValue未知运算符,mongodb,Mongodb,数据如下: { "_id" : { "$oid" : "546b79a2e4b0f7bfbaa97cc7" }, "title" : "Eyewitness: Highlands, Scotland", "description" : "Photographs from the Guardian Eyewitness series", "timeStamp" : "14/11/2014", "category" : "news", "ur
{
"_id" : { "$oid" : "546b79a2e4b0f7bfbaa97cc7" },
"title" : "Eyewitness: Highlands, Scotland",
"description" : "Photographs from the Guardian Eyewitness series",
"timeStamp" : "14/11/2014",
"category" : "news",
"url" : "http://www.theguardian.com/world/picture/2014/nov/14/1",
"source" : "http://www.theguardian.com/",
"mainStory" : "\n",
"keywords" : [ "Wildlife", "Scotland" ]
}
但是,当我使用下面的命令查找某个内容时,就会出现错误
db.guardian.find({ "_id": {"$oid": '546b79a2e4b0f7bfbaa97cc7'}})
如何找到具有特定
$oid
的文档您需要将id字符串转换为如下所示的ObjectId:
db.guardian.find({ "_id": ObjectId("546b79a2e4b0f7bfbaa97cc7") })
原因是{“$oid”:“546B79A2E4B0F7FBAA97CC7”
与ObjectId(“546B79A2E4B0F7FBAA97CC7”)相同,只是格式不同而已
有关更多详细信息,请参阅。您可以向模型中添加以下类方法
def self.serialize_from_session(key, salt)
(key = key.first) if key.kind_of? Array
(key = BSON::ObjectId.from_string(key['$oid'])) if key.kind_of? Hash
record = to_adapter.get(key)
record if record && record.authenticatable_salt == salt
end
def self.serialize_into_session(record)
[record.id.to_s, record.authenticatable_salt]
end
如果您使用的是nodejs,那么还需要使用变量ObjectID=require('mongodb').ObjectID要求ObjectID;