1

my document has a field called 'tags', which is an array of string. I'm trying to search for documents that contains 'keyword' in 'tags'. However, this doesn't seem to work on Azure Cosmos DB. Any help is appreciated.

Edit: I replaced my previous post with more complete code sample, as one of the commenters requested.

const MongoClient = require('mongodb').MongoClient;
const mongoConnectionString = process.env.MONGODB_CONNECTION_STRING;
const mongoOptions =
{
    connectTimeoutMS: 0
}

var mongoDB;

MongoClient.connect(mongoConnectionString, (err, db) => {

    if (err) throw err;

    mongoDB = db.db('mydb');

    mongoDB.collection('mycol', (err, collection) => {            
        if (err) throw err;            
        collection.find(
            {tags: {$in:['keyword']}},
            (err, cursor) => {
                if (err) throw(err);
                cursor.toArray((err, docs) => {
                    for (i = 0; i < docs.length; i++) {
                        console.log(JSON.stringify(docs));
                        db.close();
                    }
                });
            }
        );
    });
});
  • The argument to `$in` is meant to be an array, You are just sending a string. You probably just want `{ tags: 'keyword' }` anyway. You don't need a special operator just to match items "stored" in an array. It's the other way around, being a "list of possible values" to match. – Neil Lunn Aug 26 '17 at 07:54
  • I have tried { tags: 'keyword' }. It also doesn't work. –  Aug 26 '17 at 07:57
  • Also take a look at the actual API spec for [`.find()`](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#find) since you are not supplying valid arguments. `.sort()` and `.limit()` should be used as separate cursor modifiers. Also stop making reactionary edits. You copy and paste your actual code when asking a question. How hard can that be? If you keep changing things and claiming you still have a problem, then we find it difficult to believe that you actually ran it that way in the first place. – Neil Lunn Aug 26 '17 at 07:59
  • See [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and follow the steps included before asking. As noted, it also helps to actually look at the documentation when we can easily just point you to it and say your usage simply does not match the method signatures. – Neil Lunn Aug 26 '17 at 08:00
  • And look at the documentation of [`.toArray()`](http://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html#toArray) and [Cursor](http://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html) in general for that matter. These are the things that act on callbacks and promises, and not where you have put them. You cannot `return cursor.toArray()` but rather that is where your callback should be. You really need to be reading these documentation links because all the examples there show clear usage examples that you are not following. – Neil Lunn Aug 26 '17 at 08:12
  • Have you actually tried reproducing the bug with Cosmos DB? Because your comments aren't that helpful either. –  Aug 26 '17 at 08:22
  • There is no bug. It's a usage problem. Read the documentation. Half of the problem is incorrect arguments, the other half is basically another case of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). So if you actually read the documentation and follow the same usage examples then you would not have the problem. That's what helpful means. – Neil Lunn Aug 26 '17 at 08:25
  • I reposted with the complete minimal verifiable example, as you requested. It still does not work. I don't think this is a problem with usage. –  Aug 26 '17 at 08:47
  • See all those pieces of "blue text" in the comments above? Those are links that you really needed to click on and read. If you did you would have seen the many examples in the documentation structured like this: `db.collection('mycol').find({tags: {$in:['keyword']}}).toArray((err,callback) => { ... })`. The only way you could miss that is by completely ignoring those given links. So by ignoring the documentation you are just compounding error upon error. Also "complete" usually needs a data set which should actually match the query parameters as well. – Neil Lunn Aug 26 '17 at 08:54
  • I have also tried that... It still doesn't work. I think you are ignoring the fact that this question is not only about mongodb, but also about cosmos db... –  Aug 26 '17 at 08:55

1 Answers1

0

The magic trick with cosmos DB is using $elemMatch. case closed.

mongoDB.collection('mycol', (err, collection) => {            
    if (err) throw err;            
    collection.find({tags: {$elemMatch:{$in : ['keyword']}}}).toArray((err, docs) => {
        console.log(docs);
    });
});