简体   繁体   中英

Mongoose query into an embedded document

I´ve defined the following schema with 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. The other Schema is more is simpler, without any embedded document.

The strange thing is that the following works:

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

Is there any Schema error? 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.

2. Mongoose has problems querying to documents that embed complex parameters such as another document.

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.

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:

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

I' m worried that Mongoose do not returns an empty response with zero results. 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. 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'. The queries delayed too much and Mongoose or MongoDB didn´t alert any timeout warning or error message. After defining a compound index the query worked OK However...

  1. The queries worked OK executing them in MongoDB shell. I don' t know why with Mongoose were so slow. 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. MongoDB itself consumes a lot of resources to handle queries and responses quickly. Honestly I expected more from 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.

在第一个查询中,链接属性不在文档的“根”,您要搜索“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

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

is probably trying to find a field with name 'content.link' instead of 'content', try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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