MongoDBは、トランザクションデータを格納するための要件を簡単に処理できます。あなたの日付に適したフォーマットは、あなたのコレクションの中に、BSON Dateフォーマットで保存されていることです。これは、クエリとソートに使いやすいフォーマットです。以下の「新しい日付」を使用すると、ISODate()ラッパーを使用して日付を正しくフォーマットします。
例:2つの日付の間のすべてのドキュメントを検索するには
db.foo.find({Date: ISODate("1931-01-31")}).pretty();
{
"_id" : ObjectId("57e0a749e4901a8bc1ab69f6"),
"Quote" : "Many of life's failures are people who did not realize how close they were to success when they gave up.",
"Author" : "Thomas Edison",
"Date" : ISODate("1931-01-31T00:00:00Z")
}
サンプルクエリ:サンプルデータのちょうど1文書を返します。特定の日付で文書を見つけるため
use foo_test
db.foo.drop()
db.foo.insert(
{Quote:"Many of life's failures are people who did not realize how close they were to success when they gave up.",
Author: "Thomas Edison",
Date: new Date("1931-01-31")});
db.foo.insert(
{Quote:"In this world nothing can be said to be certain, except death and taxes.",
Author: "Benjamin Frankin",
Date: new Date("1789-11-13")});
db.foo.insert(
{Quote:"Houston, we've had a problem here",
Author: "Jim Lovell - Apollo 13 Commander",
Date: new Date("1970-04-15")});
db.foo.insert(
{Quote:"There aren't many downsides to being rich, other than paying taxes and having relatives asking for money.",
Author: "Bill Murray",
Date: new Date("2005-03-14")});
サンプルクエリサンプルデータの2つのドキュメントを返します。
db.foo.find({Date:{$gte:ISODate("1931-01-31"),$lte:ISODate("1970-05-01")}}).pretty();
{
"_id" : ObjectId("57e0a749e4901a8bc1ab69f6"),
"Quote" : "Many of life's failures are people who did not realize how close they were to success when they gave up.",
"Author" : "Thomas Edison",
"Date" : ISODate("1931-01-31T00:00:00Z")
}
{
"_id" : ObjectId("57e0a749e4901a8bc1ab69f8"),
"Quote" : "Houston, we've had a problem here",
"Author" : "Jim Lovell - Apollo 13 Commander",
"Date" : ISODate("1970-04-15T00:00:00Z")
}