在MongoDB中,使用$ in搜索是否比多次单个搜索更快?
是的,使用$in更快。让我们看一个示例并创建包含文档的集合-
> db.demo653.insertOne({subject:"MySQL"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea04b274deddd72997713c0")
}
> db.demo653.insertOne({subject:"MongoDB"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea04b304deddd72997713c1")
}
> db.demo653.insertOne({subject:"Java"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea04b354deddd72997713c2")
}
> db.demo653.insertOne({subject:"C"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea04b384deddd72997713c3")
}
> db.demo653.insertOne({subject:"C++"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea04b3b4deddd72997713c4")
}在find()方法的帮助下显示集合中的所有文档-
> db.demo653.find();
这将产生以下输出-
{ "_id" : ObjectId("5ea04b274deddd72997713c0"), "subject" : "MySQL" }
{ "_id" : ObjectId("5ea04b304deddd72997713c1"), "subject" : "MongoDB" }
{ "_id" : ObjectId("5ea04b354deddd72997713c2"), "subject" : "Java" }
{ "_id" : ObjectId("5ea04b384deddd72997713c3"), "subject" : "C" }
{ "_id" : ObjectId("5ea04b3b4deddd72997713c4"), "subject" : "C++" }以下是使用$in和比多个单次搜索更快的查询的查询-
> db.demo653.find({subject:{$in:["MySQL","C++","C"]}});这将产生以下输出-
{ "_id" : ObjectId("5ea04b274deddd72997713c0"), "subject" : "MySQL" }
{ "_id" : ObjectId("5ea04b384deddd72997713c3"), "subject" : "C" }
{ "_id" : ObjectId("5ea04b3b4deddd72997713c4"), "subject" : "C++" }热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下