MongoDB 查询筛选仅包含内容中单词“work”的日志
要筛选包含单词“work”的日志,请使用 aggregate() 和 $filter。我们首先创建一个包含文档的集合 −
> db.demo383.insertOne(
... {
... "ServerName":"Jboss",
... "ServerLogs": [
... {
... "status":"Working"
... },
... {
... "status":"Stop"
... },
... {
... "status":"Worked"
... }
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e5b635422064be7ab44e7f1")
}在 find() 方法的帮助下显示集合中的所有文档 −
> db.demo383.find().pretty();
这将产生以下输出 −
{
"_id" : ObjectId("5e5b635422064be7ab44e7f1"),
"ServerName" : "Jboss",
"ServerLogs" : [
{
"status" : "Working"
},
{
"status" : "Stop"
},
{
"status" : "Worked"
}
]
}以下是筛选查询 −
> db.demo383.aggregate([
... { "$addFields": {
... "ServerLogs": {
... "$filter": {
... "input": "$ServerLogs",
... "cond": {
... "$ne": [
... { "$indexOfBytes": [
... { "$toUpper": "$$this.status" },
... { "$toUpper": "work" }
... ]},
... -1
... ]
... }
... }
... }
... }}
... ])这将产生以下输出 −
{
"_id" : ObjectId("5e5b635422064be7ab44e7f1"), "ServerName" : "Jboss", "ServerLogs" : [
{ "status" : "Working" }, { "status" : "Worked" }
]
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP