Javaを使用してMongoDBコレクションのドキュメントを並べ替える方法は?
MongoDBコレクションからレコードを取得しているときに、 sort()を使用して結果のレコードを並べ替えることができます。 メソッド。
db.COLLECTION_NAME.find().sort({KEY:1}) Java MongoDBライブラリは同じ名前のメソッドを提供し、レコードの数を制限して、ソートのタイプ(昇順または降順)とそれに基づくフィールド名をバイパスして(find()メソッドの結果に基づいて)このメソッドを呼び出します。レコードを次のように並べ替えたい-
sort(Sorts.ascending("age"); 例
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Sorts;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.bson.Document;
import com.mongodb.MongoClient;
public class SortingRecords {
public static void main( String args[] ) {
//Creating a MongoDB client
MongoClient mongo = new MongoClient( "localhost" , 27017 );
//Connecting to the database
MongoDatabase database = mongo.getDatabase("myDatabase");
//Creating a collection object
MongoCollection<Document> collection = database.getCollection("students");
Document document1 = new Document("name", "Ram").append("age", 26).append("city", "Hyderabad");
Document document2 = new Document("name", "Robert").append("age", 27).append("city", "Vishakhapatnam");
Document document3 = new Document("name", "Rhim").append("age", 30).append("city", "Delhi");
Document document4 = new Document("name", "Radha").append("age", 28).append("city", "Mumbai");
Document document5 = new Document("name", "Ramani").append("age", 45).append("city", "Pune");
//Inserting the created documents
List<Document> list = new ArrayList<Document>();
list.add(document1);
list.add(document2);
list.add(document3);
list.add(document4);
list.add(document5);
collection.insertMany(list);
System.out.println("Documents Inserted");
//Retrieving a collection object
collection = database.getCollection("students");
//Retrieving the documents with a limit
FindIterable<Document> iterDoc =
collection.find().sort(Sorts.ascending("age"));
Iterator it = iterDoc.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
} 出力
Documents Inserted
Document{{_id=5e88843d5c851b345fce2a33, name=Ram, age=26, city=Hyderabad}}
Document{{_id=5e88843d5c851b345fce2a34, name=Robert, age=27, city=Vishakhapatnam}}
Document{{_id=5e88843d5c851b345fce2a36, name=Radha, age=28, city=Mumbai}}
Document{{_id=5e88843d5c851b345fce2a35, name=Rhim, age=30, city=Delhi}}
Document{{_id=5e88843d5c851b345fce2a37, name=Ramani, age=45, city=Pune}} -
Javaを使用してMongoDBからデータを取得しているときにドキュメントをスキップするにはどうすればよいですか?
MongoDBコレクションからレコードを取得しているときに、 skip()を使用して結果のレコードをスキップできます。 メソッド。 構文 db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER) Java MongoDBライブラリは、同じ名前のメソッドを提供します。レコードをスキップするには、スキップするレコードの数を表す整数値をバイパスして、このメソッドを(find()メソッドの結果で)呼び出します。 例 import com.mongodb.client.FindIterable; import com.mongodb.client.Mon
-
Javaを使用してMongoDBデータベース内のすべてのコレクションを一覧表示するにはどうすればよいですか?
show collectionsを使用して、データベース内の既存のすべてのコレクションのリストを印刷できます。 例 以下に示すように、MongoDBデータベースに3つのコレクションを作成したと仮定します- > use sampleDatabase switched to db sampleDatabase > db.createCollection("students") { "ok" : 1 } > db.createCollection("teachers") { "ok" : 1 }