db.collection findOne by id 运行但不返回
db.collection findOne by id runs but doesn't return
我正在使用节点mongo db本机模块,但我在使用findOne时遇到了困难。
当我运行时:
let collection = db.collection('eventschemas')
collection.find({}).toArray(function(error, result) {
if (error) console.log(error);
console.log(result);
});
结果将是:
[ { _id: 56b644a1bc8f66ce59322b38,
type: 'events',
attributes:
{ hiw: [Object],
title: 'Pub Crawl',
'meeting-point': 'Ponto de encontro',
information: '1h de Open Bar',
'women-price': 40,
'men-price': 60,
end: Sat Feb 13 2016 17:08:00 GMT-0200 (BRST),
start: Sat Feb 13 2016 17:07:59 GMT-0200 (BRST),
'updated-at': Sat Feb 06 2016 17:08:54 GMT-0200 (BRST),
'created-at': Sat Feb 06 2016 17:08:17 GMT-0200 (BRST),
'is-active': true },
__v: 0 } ]
我只有一个事件,所以这是正确的结果。但是当我运行时:
let collection = db.collection('eventschemas')
collection.findOne({ _id: '56b644a1bc8f66ce59322b38' }, function(error, result) {
if (error) console.log(error);
console.log(result);
});
结果将是空的,这没有意义。我错过了什么吗?
谢谢。
尝试将_id
字符串包装在 ObjectID()
中:
var ObjectID = require('mongodb').ObjectID,
id = new ObjectID('56b644a1bc8f66ce59322b38'); // wrap in ObjectID
let collection = db.collection('eventschemas')
collection.findOne({ _id: id }, function(error, result) {
if (error) console.log(error);
console.log(result);
});
相关文章:
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- 谷歌地图:函数返回未定义的值在console.log中运行良好
- 在promise完成后,依次运行返回promise的函数
- Scala提升了如何运行javascript函数并将返回值存储在变量中
- javascript在html元素方法运行setTimeout后返回此消息
- 使用 jQuery 从运行 MySQL 的 PHP 脚本返回信息
- 如何在运行总计计算器时返回表单输入并更新 JavaScript 变量
- $.getJSON 在返回数据后不会运行函数
- 动态弹出窗口'通过调用一个永远运行并返回's每个周期后的数据
- 自定义Angular.js过滤器在IE8中不返回任何内容,在Chrome/FF中运行良好
- 如何在返回另一个函数中的值后运行一个函数
- 未运行通过AJAX从PHP返回的Javascript代码
- 当在运行nodejs的服务器上调用new Date()时,Date将返回服务器启动时的日期
- 从Java脚本返回多个值,这些值不能在firefox上运行(在IE上运行良好)
- Ajax调用在本地返回json,但在服务器上运行时返回xml
- 自定义Div滑块运行过快,但仅当从另一个浏览器选项卡返回时
- 然后在ajax返回之前运行
- UIWebView 无法将 JavaScript 作为 document.getElementById 运行,返回 nu
- 运行 grunt.file.copy 返回错误代码:ENOENT
- 如何将javascript变量传递给php文件,运行数据库查询并返回true或false