简体   繁体   中英

Using mongojs and node.js, how to find documents using multiple attributes as criteria

I am trying to use the find method to return documents that meet multiple criteria like so:

db.issues.find({ 'title.name': name, 'title.year': year, 'title.publisher': publisher }, function(err, issues) { ...

I get an empty array back as a result.

If I try the same query directly in the mongodb console, I get the results back as expected.

In mongojs, I am able to retrieve results if I only supply one attribute, like so:

db.issues.find({ 'title.name': name }, function(err, issues) { ...

So, how do I perform a search using multiple attributes? Do I need to use a different method?

UPDATED 2/2/2014 7:56 The issue schema is as follows:

{
    title: { name: 'Some title name', year: 1999, publisher: 'Some publisher' },
    number: "122", 
    variant: null,
    print: "1",
    year: "1999",
    certification: { type: "Some certification", grade: 10 },
    url: "http://www.someurl.com",
    sales: []
}

There is no difference in how mongojs handles queries as it's intention is to mimic ( somewhat ) the behavior of the mongo shell.

Are you sure your criteria actually returns something? As it stands you possibly have an issue there. If you still cannot diagnose this, post up the result you expect to get and some of your schema as an edit. Don't forget to check your var's that numbers aren't strings and vice versa.

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