Underscore's findWhere and MongoDB object
Underscore's findWhere and MongoDB object
我使用MongoDB,我想在我的Angular应用程序中使用Underscore.js。我有一个模型"电子邮件",其中包含以下字段:"主题","正文"和"id"。
此电子邮件对象在浏览器控制台中如下所示:
$$hashKey
"005"
_id
Object { $oid="5478774a6a61734d8a000000"}
body
"Here is the sample conte...le ble ble. Nice email."
subject
"This is first template"
这是 Angular 中的控制器代码:
$scope.viewEmail = function(emailId) {
var email = _.findWhere(emailData.data.emails, { _id: emailId });
console.log(email);
};
所以,我只想找到一封包含特定id
的电子邮件 - 我想得到的id
保存在局部变量emailId
中。
我应该做这样的事情:_id.$oid: emailId
但它会在 javascript 控制台中导致语法错误:"缺少:在属性 ID 之后"。
这将完成这项工作: _.find(list, predicate, [context])
这是一个小提琴:http://jsfiddle.net/bqegdy2t/
var emailData = {
data: {
emails: [
{ _id: { $oid: "someId" }, body: "Hello Every Body1" },
{ _id: { $oid: "someOtherId" }, body: "Hello Every Body2" },
]
}
};
var emailId = "someId";
var email = _.find(emailData.data.emails, function(email) {
return email._id.$oid === emailId;
});
console.log(email);
困难在于 mongo objectid 格式,这是一个包含字符串的不寻常的 JSON 对象。
尝试_id: emailId
与_id.$oid: emailId
另一种选择:点表示法通常需要引号"_id.$oid" : emailId
尤其是带有美元符号的点符号
相关文章:
- 如何缩短MongoDB ObjectId并在Mongoose模式中使用它
- 如何处理node.js节点mongodb中的连接和查询队列
- MongoDB (php) - 以数组而不是多个属性的形式返回文档属性
- Selenium WebDriver and JavaScript change
- MongoDB MapReduce, Date and % operator
- Ajax and Mongodb, use _id
- Underscore's findWhere and MongoDB object
- MongoDB and GridFS
- Count Undefined with MongoDB and Nodejs
- ElasticSearch and MongoDB
- async.parallel on mongodb and Node.js
- 如何使用MongoDB/ Mongoose.js查询OR/ and类条件
- FindRecord with Ember and MongoDB
- mongodb group and sum?
- Node.js, MongoDB, and Concurrency
- Loopback and MongoDB
- NodeJS, express and mongoDB - mapping logic
- MongoDB int64 and JavaScript
- MongoDB, PHP and JavaScript
- MongoDB Double Upsert and a Push