$slice演算子を使用してMongoDBの配列の最後の要素を取得するにはどうすればよいですか?
MongoDBで配列の最後の要素を取得するには、次の構文を使用します
db.yourCollectionName.find({},{yourArrayFieldName:{$slice:-1}}); まず、ドキュメントを使用してコレクションを作成しましょう
>db.getLastElementOfArrayDemo.insertOne({"StudentName":"James","StudentMathScore":[78,68,98]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9d2d71a629b87623db1b2e")
}
>db.getLastElementOfArrayDemo.insertOne({"StudentName":"Chris","StudentMathScore":[88,56,34]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9d2d83a629b87623db1b2f")
}
>db.getLastElementOfArrayDemo.insertOne({"StudentName":"Larry","StudentMathScore":[99]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9d2d8ea629b87623db1b30")
}
>db.getLastElementOfArrayDemo.insertOne({"StudentName":"Robert","StudentMathScore":[90,78,67,66,75,73]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9d2dada629b87623db1b31")
}> 以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです
> db.getLastElementOfArrayDemo.find().pretty();
これにより、次の出力が生成されます
{
"_id" : ObjectId("5c9d2d71a629b87623db1b2e"),
"StudentName" : "James",
"StudentMathScore" : [
78,
68,
98
]
}
{
"_id" : ObjectId("5c9d2d83a629b87623db1b2f"),
"StudentName" : "Chris",
"StudentMathScore" : [
88,
56,
34
]
}
{
"_id" : ObjectId("5c9d2d8ea629b87623db1b30"),
"StudentName" : "Larry",
"StudentMathScore" : [
99
]
}
{
"_id" : ObjectId("5c9d2dada629b87623db1b31"),
"StudentName" : "Robert",
"StudentMathScore" : [
90,
78,
67,
66,
75,
73
]
} 以下は、MongoDBの配列の最後の要素を取得するためのクエリです
> db.getLastElementOfArrayDemo.find({},{StudentMathScore:{$slice:-1}}); これにより、次の出力が生成されます
{ "_id" : ObjectId("5c9d2d71a629b87623db1b2e"), "StudentName" : "James", "StudentMathScore" : [ 98 ] }
{ "_id" : ObjectId("5c9d2d83a629b87623db1b2f"), "StudentName" : "Chris", "StudentMathScore" : [ 34 ] }
{ "_id" : ObjectId("5c9d2d8ea629b87623db1b30"), "StudentName" : "Larry", "StudentMathScore" : [ 99 ] }
{ "_id" : ObjectId("5c9d2dada629b87623db1b31"), "StudentName" : "Robert",
"StudentMathScore" : [ 73 ] } -
JavaScript配列の最後の要素を取得するにはどうすればよいですか?
JavaScript配列の最後の要素を取得するには、slice()メソッドを使用します。 例 次のgetthelastelementを実行してみてください。 <html> <body> <script> var array = [30,40,50,60];  
-
AndroidのConcurrentLinkedDequeの最後の要素を取得するにはどうすればよいですか?
例に入る前に、ConcurrentLinkedDequeisが何であるかを知っておく必要があります。これは、リンクされたノードに基づく無制限の両端キューです。複数のスレッドが安全に両端キュー要素にアクセスできます。 この例は、Androidの最後の要素を取得する方法を示していますConcurrentLinkedDeque ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main