[英]mongo db.find not working
I have the following script that I'm running through mongo shell. 我有以下通过mongo shell运行的脚本。 The first posts query executes fine and I receive the data object.
第一个帖子查询执行正常,我收到了数据对象。 Also if I print the date after the convertDate function, the correct date is returned.
另外,如果我在convertDate函数之后打印日期,则返回正确的日期。 However in the second postdata findOne function I can't get any of the print statements to show anything.
但是在第二个postdata findOne函数中,我无法获得任何打印语句来显示任何内容。 So there isn't an error but no results are found which should still show 'no record'.
因此,没有错误,但是没有找到结果,但仍应显示“无记录”。 Am I missing something here?
我在这里想念什么吗?
db.posts.find().forEach(function(data) {
var date = convertDate(data.created_at);
db.postdata.findOne({ company_id: data.company_id, date: date }, function(err, post){
if (err) { print('err' + err) }
if(post){
print('test' + post);
} else {
print('no record');
}
});
});
findOne
in the shell is synchronous so the found post is returned rather than passed into a callback. 外壳程序中的
findOne
是同步的,因此返回找到的帖子,而不是将其传递给回调。
db.posts.find().forEach(function(data) {
var date = convertDate(data.created_at);
var post = db.postdata.findOne({ company_id: data.company_id, date: date });
if(post){
print('test' + post);
} else {
print('no record');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.