2017-11-21 5 views
0

この質問はパフォーマンスに基づいています。MongoDBのクエリデータとコードのフィルタリング

複数のフィールド(fieldValue < x < fieldValue、status = 'pending'など)で照会したいコレクションがある場合は、mongoDBクエリを使用してクエリを実行するか、 status = 'pending'のような単純なクエリに適合し、サーバーコードのデータをさらにフィルタリングするコレクション?

いつどのような方法をお勧めしますか?

ありがとうございます。不要なデータのほとんどと、データベースから必要なデータを取得するフィルタリング以来

よろしく、単一のクエリオプションの エミール

答えて

0

Goが、データベース自体に行われるべきです。追加の操作には、同じジョブを完了するための独自の時間とリソースが必要です。ここでは、$and,$gt$lt$eqを使用できます。データレイヤーでデータを操作すると、パフォーマンスが向上します。

サンプル収集

{ "_id" : ObjectId("5a13c7e08e1b021d0f556c29"), "value" : 10, "status" : "pending" } 
{ "_id" : ObjectId("5a13c7e58e1b021d0f556c2a"), "value" : 20, "status" : "completed" } 
{ "_id" : ObjectId("5a13c7e88e1b021d0f556c2b"), "value" : 40, "status" : "In Progress" } 
{ "_id" : ObjectId("5a13c7ec8e1b021d0f556c2c"), "value" : 50, "status" : "pending" } 
{ "_id" : ObjectId("5a13c7f08e1b021d0f556c2d"), "value" : 750, "status" : "completed" } 
{ "_id" : ObjectId("5a13c7f68e1b021d0f556c2e"), "value" : 90, "status" : "pending" } 
{ "_id" : ObjectId("5a13c7fb8e1b021d0f556c2f"), "value" : 190, "status" : "pending" } 
{ "_id" : ObjectId("5a13c7fe8e1b021d0f556c30"), "value" : 120, "status" : "completed" } 
{ "_id" : ObjectId("5a13c8038e1b021d0f556c31"), "value" : 220, "status" : "completed" } 
{ "_id" : ObjectId("5a13c8078e1b021d0f556c32"), "value" : 720, "status" : "pending" } 
{ "_id" : ObjectId("5a13c80b8e1b021d0f556c33"), "value" : 7420, "status" : "In Progress" } 

は、サンプルクエリ:20 < X < 300及びステータス=

db.collection.find({$and:[{value:{$gt: 20}, value:{$lt:300}, status:{$eq:"pending"}}]}) 

ペンディング結果は

{ "_id" : ObjectId("5a13c7e08e1b021d0f556c29"), "value" : 10, "status" : "pending" } 
{ "_id" : ObjectId("5a13c7ec8e1b021d0f556c2c"), "value" : 50, "status" : "pending" } 
{ "_id" : ObjectId("5a13c7f68e1b021d0f556c2e"), "value" : 90, "status" : "pending" } 
{ "_id" : ObjectId("5a13c7fb8e1b021d0f556c2f"), "value" : 190, "status" : "pending" } 
であろう

希望すると助かります!

関連する問題