$regex support in Stitch - using mongodb-stitch-react-native-sdk

9 views
Skip to first unread message

jug smith

unread,
Jun 22, 2019, 1:28:47 AM6/22/19
to MongoDB Stitch Users
Hi guys,

Is there a different way of using $regex with Stitch while using the react-native version of the SDK?  Or is there a different way I should use  $regex?

I'm able to use $regex from the Mongo Shell.. but I don't get the correct results back when I use it from my react-native app.
I get some results back but they're not filtered by my $regex query below. 

---
import { BSON } from 'mongodb-stitch-react-native-sdk';
const numposts = 10;
const queryString = 'versatile';
const regexp = new BSON.BSONRegExp(queryString, 'i');
const query = 
{
$or: [
{ "name" : { $regex: regexp, $options: "i" }},
{ "title" : { $regex: regexp, $options: "i" }},
{ "brand" : { $regex: regexp, $options: "i" }},
{ "details": { $regex: regexp, $options: "i" }}, // this is an array, everything above is an individual string field
]
};
const coll = db.collection( itemsCollection ); 
const options = {"sort": {"_id": -1}, "limit": numposts};
const prom = coll.find(query, options).toArray().then( items => {
console.log(items);
});

Thanks,
Raj
--
Results while using the Mongo DB Shell

> db.items.find({ "brand": { $regex: "Lands end" } }).count()
0
> db.items.find({ "brand": { $regex: "Lands end", $options: "i" } }).count()
32
// details is an array, everything above is an individual string field
> db.items.find({ "details.0": { $regex: "Classic", $options: "i" } }).count()
22
> db.items.find({ "details": { $regex: "Classic", $options: "i" } }).count()
88
> db.items.find({ "details": { $regex: "versatile", $options: "i" } }).count()
16
> db.items.find({ "details.1": { $regex: "Classic", $options: "i" } }).count()
18
> db.items.find({ "details.2": { $regex: "Classic", $options: "i" } }).count()
26
> db.items.find({ "details.3": { $regex: "Classic", $options: "i" } }).count()
14
> db.items.find({ "details.4": { $regex: "Classic", $options: "i" } }).count()
7
> db.items.find({ "details.5": { $regex: "Classic", $options: "i" } }).count()
2
> db.items.find({ "details.6": { $regex: "Classic", $options: "i" } }).count()
> const regex = new RegExp( 'classic' );
> db.items.find({ "details": { $regex: regex , $options: "i" } }).count()
88

Any help or pointers would be appreciated. Thanks
J
Reply all
Reply to author
Forward
0 new messages