MongoDB 查询以查找数组中存在作为指定词子串的字符串的文档
对于此类评估,请在 MongoDB 中使用 aggregate()。我们创建一个带有文档的集合 -
> db.demo90.insertOne(
... {"words": ["john", "jace"]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e2c1ada79799acab037af56")
}
> db.demo90.insertOne(
... {"words": ["sam", "adam"]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e2c1adb79799acab037af57")
}在集合中使用 find() 方法显示所有文档 -
> db.demo90.find();
这将生成以下输出 -
{ "_id" : ObjectId("5e2c1ada79799acab037af56"), "words" : [ "john", "jace" ] }
{ "_id" : ObjectId("5e2c1adb79799acab037af57"), "words" : [ "sam", "adam" ] }以下是查找数组中存在作为特定词子串的字符串的文档的查询 -
> db.demo90.aggregate([ { $match: { $expr: { $anyElementTrue: { $map: { input: "$words", as: "j", in: { $ne: [ -1, { $indexOfBytes: [ "john", "$$j" ] } ] } } } } } } ]);这将生成以下输出 -
{ "_id" : ObjectId("5e2c1ada79799acab037af56"), "words" : [ "john", "jace" ] }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP