使用PyMongo执行正则表达式查询?
PyMongo是一个Python发行版,其中包含用于MongoDB的工具。要使用PyMongo执行正则表达式查询,语法如下-
db.yourCollectionName.find({'yourCollectionName':{'$regex':'^yourWords'}}).pretty();
上面的语法将提供所有从特定单词开始的文档。
为了理解语法,让我们用文档创建一个集合。使用文档创建集合的查询如下-
> db.performRegex.insertOne({"ClientName":"Larry","ClientFolderInformation":["Folder 1","Folder 2","Folder 3","Folder 4","Folder 5"], "MainFolderLocation":"/MainFolder/Details/ClientFolder" });
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a8b186cea1f28b7aa07f2")
}
> db.performRegex.insertOne({"ClientName":"Larry","ClientFolderInformation":[
"ClientFolder 1","ClientFolder 2","ClientFolder 3","ClientFolder 4","ClientFolder 5"],
"MainFolderLocation":"/MainFolder/Details/ClientFolder" });
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a8b1d6cea1f28b7aa07f3")
}
在find()
method的帮助下显示集合中的所有文档。查询如下-
> db.performRegex.find().pretty();
以下是输出-
{"_id" : ObjectId("5c8a8b186cea1f28b7aa07f2"),
"ClientName" : "Larry",
"ClientFolderInformation" : [
"Folder 1",
"Folder 2",
"Folder 3",
"Folder 4",
"Folder 5"
],
"MainFolderLocation" : "/MainFolder/Details/ClientFolder"
}
{
"_id" : ObjectId("5c8a8b1d6cea1f28b7aa07f3"),
"ClientName" : "Larry",
"ClientFolderInformation" : [
"ClientFolder 1",
"ClientFolder 2",
"ClientFolder 3",
"ClientFolder 4",
"ClientFolder 5"
],
"MainFolderLocation" : "/MainFolder/Details/ClientFolder"
}
这是执行正则表达式的查询。
情况1-以下查询给出了所有以'ClientFolder'开头的文档-
> db.performRegex.find({'ClientFolderInformation':{'$regex':'^ClientFolder'}}).pretty();
以下是输出-
{"_id" : ObjectId("5c8a8b1d6cea1f28b7aa07f3"),
"ClientName" : "Larry",
"ClientFolderInformation" : [
"ClientFolder 1",
"ClientFolder 2",
"ClientFolder 3",
"ClientFolder 4",
"ClientFolder 5"
],
"MainFolderLocation" : "/MainFolder/Details/ClientFolder"
}
情况2-以下查询提供了所有以“文件夹”开头的文档-
> db.performRegex.find({'ClientFolderInformation':{'$regex':'^Folder'}}).pretty();
以下是输出-
{"_id" : ObjectId("5c8a8b186cea1f28b7aa07f2"),
"ClientName" : "Larry",
"ClientFolderInformation" : [
"Folder 1",
"Folder 2",
"Folder 3",
"Folder 4",
"Folder 5"
],
"MainFolderLocation" : "/MainFolder/Details/ClientFolder"
}
以上是 使用PyMongo执行正则表达式查询? 的全部内容, 来源链接: utcz.com/z/322372.html