lcappa
lcappa

Reputation: 329

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!

Upvotes: 3

Views: 1460

Answers (4)

lcappa
lcappa

Reputation: 329

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 O.K. However...

  1. The queries worked O.K 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.

Upvotes: 2

yngccc
yngccc

Reputation: 5684

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)

Upvotes: 0

shelman
shelman

Reputation: 2699

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

Upvotes: 0

supernova
supernova

Reputation: 3883

in the 1st query, the link attribute isn't at the "root" of your document, you want to search for "content.link" instead of "link" ( i don't know if the current mongoose version supports the "." to find something in an embedded document )

Upvotes: 0

Related Questions