Mongodb find 不适用于 Objectid [英] Mongodb find is not working with the Objectid

查看:51
本文介绍了Mongodb find 不适用于 Objectid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,我得到了文档的空值.但是在 findOne({'webpageid':docs[i]._id} 而不是 docs[i]._id 如果我传递了 webpageid 它可以工作.并且我验证了 docs 具有所需的网页数据

//获取网页表中的所有记录var collection = db.get('网页');collection.find({}, function(e,docs){//遍历网页列表for (var i = 0; i < docs.length; i++) {//检查结果表是否有此网页的任何记录db.get('results').findOne({'webpageid':docs[i]._id}, function(err, doc){如果(错误){返回;}控制台日志(文档);});};});

以下是网页"表中的内容

{"_id" : ObjectId("549608e16ecb16dc3c4880e6"),名称":空,"url" : "http://www.google.com",texttoverify":空,间隔":空,"websiteid" : null}{"_id" : ObjectId("549609986ecb16dc3c4880e7"),名称":空,"url" : "http://www.google.com",texttoverify":空,间隔":空,"websiteid" : null}{"_id" : ObjectId("54960a916ecb16dc3c4880e8"),"name" : "xyz","url" : "http://www.google.com","texttoverify" : "你好",间隔":00:15:00.0000000",网站ID":02D7BE81-4E01-4347-BAE5-BA9A2D7EE11E"}

这是我在结果"表中的内容

{"_id" : ObjectId("54985a1e69af082f9c477574"),网页ID":549608e16ecb16dc3c4880e6"}{_id":ObjectId(54986f4e69af082f9c477575"),名称":名称"}{"_id" : ObjectId("5498d10c69af082f9c477576"),"姓名": "姓名","webpageid" : "\"54960a916ecb16dc3c4880e8"}{"_id" : ObjectId("5498d1f969af082f9c477577"),"姓名": "姓名",网页ID":54960a916ecb16dc3c4880e8"}

感谢您的调查并感谢您的帮助.

解决方案

您的网页 ID 是字符串,而您正在搜索的 _id 是 ObjectId.您的查找结果与任何结果都不匹配,因为结果表中没有包含webpageid"元素的 ObjectId 值的文档.

我认为有两种解决方案.

  1. 您可以为 results 集合中的 webpageid 元素存储 ObjectIds 而不是字符串.这当然取决于这些文档如何进入集合.
  2. 你可以从返回的 ObjectId 在循环中进行比较.例如,

    <预><代码>...for(var i = 0; i < docs.length; i++) {var docId = docs[i]._id.toString();//创建一个字符串db.get('results').findOne({'webpageid':docId}, function(err, doc)...

如果您选择第二个选项,您可能需要查看为什么 resultswebpageid 值的开头有一个前导引号> 收藏.

"webpageid" : "\"54960a916ecb16dc3c4880e8"

最后,我不太了解您的要求,但您可能希望重新考虑 MongoDB 作为解决方案.看起来您正在创建类似 JOIN 的东西,而 MongoDB 设计的不是很好.

Here is my code and I'm getting null value for the doc. But in the findOne({'webpageid':docs[i]._id} instead of docs[i]._id if I pass the webpageid it works. And I verified docs has the required webpage data

// get all the records from the webpages table
var collection = db.get('webpages');
collection.find({}, function(e,docs){
	// iterate over the webpage list
	for (var i = 0; i < docs.length; i++) {
		// check if results table has any record for this webpage
		db.get('results').findOne({'webpageid':docs[i]._id}, function(err, doc)
		{
			if(err)
			{
				return;
			}
			console.log(doc);
		});
	};
});

Below is the content in the 'webpages' table

{
        "_id" : ObjectId("549608e16ecb16dc3c4880e6"),
        "name" : null,
        "url" : "http://www.google.com",
        "texttoverify" : null,
        "interval" : null,
        "websiteid" : null
}
{
        "_id" : ObjectId("549609986ecb16dc3c4880e7"),
        "name" : null,
        "url" : "http://www.google.com",
        "texttoverify" : null,
        "interval" : null,
        "websiteid" : null
}
{
        "_id" : ObjectId("54960a916ecb16dc3c4880e8"),
        "name" : "xyz",
        "url" : "http://www.google.com",
        "texttoverify" : "hello",
        "interval" : "00:15:00.0000000",
        "websiteid" : "02D7BE81-4E01-4347-BAE5-BA9A2D7EE11E"
}

Here is my content inside 'results' table

{
        "_id" : ObjectId("54985a1e69af082f9c477574"),
        "webpageid" : "549608e16ecb16dc3c4880e6"
}
{ "_id" : ObjectId("54986f4e69af082f9c477575"), "name" : "name" }
{
        "_id" : ObjectId("5498d10c69af082f9c477576"),
        "name" : "name",
        "webpageid" : "\"54960a916ecb16dc3c4880e8"
}
{
        "_id" : ObjectId("5498d1f969af082f9c477577"),
        "name" : "name",
        "webpageid" : "54960a916ecb16dc3c4880e8"
}

Thanks for looking into it and appreciate your help.

解决方案

Your webpageids are strings while the _ids that you are searching with are ObjectIds. Your find does not match any results because there are no documents in the results table which have ObjectId values for the "webpageid" element.

There are two solutions as I see it.

  1. You could store ObjectIds instead of strings for the webpageid element in the results collection. The implementation of this is of course based on how those documents get into the collection.
  2. You could create a string variable from the returned ObjectId within the loop to compare instead. For example,

    ...
    for(var i = 0; i < docs.length; i++) {
        var docId = docs[i]._id.toString(); // create a string
        db.get('results').findOne({'webpageid':docId}, function(err, doc)
        ...
    

If you go with the second option, you may need to look into why there is a leading quote at the beginning of the webpageid value for the second document in the results collection.

"webpageid" : "\"54960a916ecb16dc3c4880e8"

Lastly, I don't know much about your requirements, but you may want to rethink MongoDB as a solution. It appears that you are creating something like a JOIN which is something MongoDB is not designed to handle very well.

这篇关于Mongodb find 不适用于 Objectid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆