首页 > 解决方案 > Mongodb query starts with inside $in Query

问题描述

I want to write an $in query if column path starts with "/v1/user/1" or "/v1/user/2" then I want to select that particular document.

The sample document is as below:

{
    "_id" : ObjectId("5f8872b486aa9e79d71d9809"),
    "method" : "get",
    "path" : "/v1/user/1/details/"
}

I tried to create a query but getting syntax error because of "/"

db.user.find({ "path": { $in: [ /^/v1/user/1/ , /^/v1/user/2/ ] } })

Could some one please help how to write the query.

标签: mongodb

解决方案


Your query was almost correct. You just missed to escape the /. Try below query.

db.user.find({ "path": { $in: [ /^\/v1\/user\/1/ , /^\/v1\/user\/2/ ] } })

推荐阅读