简体   繁体   English

Mongoose查询到嵌入式文档

[英]Mongoose query into an embedded document

I´ve defined the following schema with Mongoose: 我用Mongoose定义了以下模式:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;

var New = new Schema({
    _id: ObjectId,
    lang: String,
    formatted: Boolean,
    downloaded: Date,
    content: {
        title: String,
        link: String,
        description: String,
        meta: String,
        author: String
    }
});

module.exports = New;

And I´m trying to execute the following query: 我正在尝试执行以下查询:

NewsModel.find({'lang':'en', 'content.link':'test'}).exec(callback);

The query doesn' t respond and it never enters into the callback function. 查询没有响应,它永远不会进入回调函数。 It' s strange, because this type of query (search into two String fields) works good with another Schema that I' ve defined, but no with this one. 这很奇怪,因为这种类型的查询(搜索到两个String字段)与我定义的另一个Schema很好用,但是没有这个。 The other Schema is more is simpler, without any embedded document. 另一个Schema更简单,没有任何嵌入式文档。

The strange thing is that the following works: 奇怪的是,以下工作:

NewsModel.find({'lang':'en', 'formatted':true}).exec(callback);

Is there any Schema error? 有任何Schema错误吗? Any idea what I' m doing wrong? 知道我做错了什么吗?

Thank you very much, 非常感谢你,

Luis Cappa. 路易斯卡帕。


[UPDATED] [更新]

I tried your suggestions, but no way. 我试过你的建议,但没办法。 I think that there are only two options: 我认为只有两种选择:

1. The Schema that I posted has something wrong. 1.我张贴的架构有一些错误。

2. Mongoose has problems querying to documents that embed complex parameters such as another document. 2. Mongoose在查询嵌入复杂参数(如另一个文档)的文档时遇到问题。

I've worked with MongoDB shell, MongoDB Java Driver and Spring MongoDB Data and that' s the first time that I experience this strange behavior. 我使用过MongoDB shell,MongoDB Java Driver和Spring MongoDB Data,这是我第一次遇到这种奇怪的行为。

The queries that I' ve tested are: 我测试的查询是:

NewsModel.find({'lang':'en', 'content.link':'test'}).exec(callback);
NewsModel.find({'lang':'en'}).where('content.link').equals('test').exec(callback);
NewsModel.find({'content.link':'test'}).where('lang').equals('en').exec(callback);
NewsModel.find({'content.link':'test'}).exec(callback); //  That demonstrates that Mongoose has problems with subelements.
NewsModel.find().where('content.link').equals('test').exec(callback); // This one too.

And one example that works perfectly with MongoDB shell: 一个与MongoDB shell完美配合的示例:

db.news.find({ 'content.link': /test/, lang: 'en' })

I' m worried that Mongoose do not returns an empty response with zero results. 我担心Mongoose不会返回空响​​应,结果为零。 Instead, it maintains the application in stand by waiting and waiting for a response and never enters at the callback function. 相反,它通过等待和等待响应来维护应用程序,并且永远不会进入回调函数。

Any ideas? 有任何想法吗? Did you experienced something similar? 你有类似的经历吗?

Thanks a lot! 非常感谢!

Resolved 解决

It was a query performance error with Mongoose. 这是Mongoose的查询性能错误。 I´ve got a test collection with about 100K documents to which execute queries and I haven' t defined a compound index with 'lang' and 'content.link'. 我有一个包含大约100K文件的测试集合,执行查询,我没有用'lang'和'content.link'定义复合索引。 The queries delayed too much and Mongoose or MongoDB didn´t alert any timeout warning or error message. 查询延迟太多,Mongoose或MongoDB没有提醒任何超时警告或错误消息。 After defining a compound index the query worked OK However... 定义复合索引后,查询工作正常但是......

  1. The queries worked OK executing them in MongoDB shell. 查询工作正常,可以在MongoDB shell中执行它们。 I don' t know why with Mongoose were so slow. 我不知道为什么Mongoose太慢了。 Maybe serializing - deserializing - validating processes involved produced that delay. 也许序列化 - 反序列化 - 验证所涉及的过程会产生延迟。

  2. Even without any index defined I can' t believe that that simple query has such poor performance with a simple test collection with only 100K documents. 即使没有定义任何索引,我也不能相信这个简单的查询具有如此差的性能,只有100K文档的简单测试集合。 MongoDB itself consumes a lot of resources to handle queries and responses quickly. MongoDB本身消耗了大量资源来快速处理查询和响应。 Honestly I expected more from MongoDB - Mongoose. 老实说,我对MongoDB的预期更多 - Mongoose。

My suggestions/advices: 我的建议/意见:

Take care about indexes configuration. 注意索引配置。

If you want quick searchs take a look to any Node.js Apache Solr module. 如果您想快速搜索,请查看任何Node.js Apache Solr模块。

在第一个查询中,链接属性不在文档的“根”,您要搜索“content.link”而不是“link”(我不知道当前的猫鼬版本是否支持“。 “在嵌入式文档中查找内容

Don't you want 你不想要吗?

NewsModel.find({'lang':'en', 'content.link':'test'}).exec(callback);

to query your embedded document? 查询您的嵌入式文档?

See this answer: 看到这个答案:

Finding an Embedded Document by a specific property in Mongoose, Node.js, MongodDB 通过Mongoose,Node.js,MongodDB中的特定属性查找嵌入式文档

NewsModel.find({'lang':'en', 'content.link':'test'}).exec(callback); 

is probably trying to find a field with name 'content.link' instead of 'content', try 可能试图找到一个名为'content.link'而不是'content'的字段,试试

NewModel.where('lang').equals('en')  
        .where('content').link.equals('test') 
        .exec(callback)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM