orlandos-nl / MongoKitten

Native MongoDB driver for Swift, written in Swift

Home Page:https://orlandos.nl/docs/mongokitten/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mongokitten 4.0 : Working with regular expression

jalajap opened this issue · comments

I have an array of documents.Please let me know the following query will work for an Aggregate.

let theQuery:Query = Query(aqt: .in(key: “User.Name", in: ["/^aso/i”, "/^asso/i”, "/^arl/i”] ))
let doc:Document = ["_id":NSNull(),"content":["$addToSet": "$User"]]

let pipeline: AggregationPipeline = [
.unwind("$User”),
.match(theQuery),
.group(groupDocument: doc)
]

let result = try collection.aggregate(pipeline)

You need a RegularExpression instead of a String

I have changed the regular expression like this,
let theQuery:Query = ["User.Name": RegularExpression(pattern:"/^aso/", options:.caseInsensitive)]

let doc:Document = ["_id":NSNull(),"content":["$addToSet": "$User"]]

let pipeline: AggregationPipeline = [
.unwind("$User”),
.match(theQuery),
.group(groupDocument: doc)
]

let result = try collection.aggregate(pipeline)

Still I am not getting the expected result. And trying the same query in terminal I am getting the result.

db.dumpCsvCollection.aggregate({"$unwind":"$User"},{"$match":{"User.Name":{$regex: /^aso/,$options: "i"}}},{"$group":{"content":{"$addToSet":"$User"},"_id":null}}).pretty()

This works. Please suggest.

Solved. Thanks

What was the solution?

@fdstevex use the BSON provided type, or a Document query using $ Mongo operators