MongoDBでさまざまな形式の特定のデータを取得するにはどうすればよいですか?
これには、単にfind()を使用します。別の形式については、pretty()を使用してください。まず、ドキュメントを使用してコレクションを作成しましょう-
> db.getSpecificData.insertOne(
... {
... "StudentName": "John",
... "Information": {
... "FatherName": "Chris",
... "Place": {
... "CountryName": "US",
... "ZipCode":"111344"
... },
... "id": "1"
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e039abdf5e889d7a5199509")
}
> db.getSpecificData.insertOne(
... {
... "StudentName": "Carol",
... "Information": {
... "FatherName": "Robert",
... "Place": {
... "CountryName": "UK",
... "ZipCode":"746464"
... },
... "id": "2"
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e039ae6f5e889d7a519950a")
}
>
> db.getSpecificData.insertOne(
... {
... "StudentName": "David",
... "Information": {
... "FatherName": "Carol",
... "Place": {
... "CountryName": "US",
... "ZipCode":"567334"
... },
... "id": "3"
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e039ae7f5e889d7a519950b")
}
>
> db.getSpecificData.insertOne(
... {
... "StudentName": "Jace",
... "Information": {
... "FatherName": "Bob",
... "Place": {
... "CountryName": "US",
... "ZipCode":"999999"
... },
... "id": "4"
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e039ae8f5e889d7a519950c")
} 以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです-
> db.getSpecificData.find({'Information.Place.CountryName':"US"}, {}, {limit: 2}, function(error, data) {}).pretty();> これにより、次の出力が生成されます-
{
"_id" : ObjectId("5e039abdf5e889d7a5199509"),
"StudentName" : "John",
"Information" : {
"FatherName" : "Chris",
"Place" : {
"CountryName" : "US",
"ZipCode" : "111344"
},
"id" : "1"
}
}
{
"_id" : ObjectId("5e039ae7f5e889d7a519950b"),
"StudentName" : "David",
"Information" : {
"FatherName" : "Carol",
"Place" : {
"CountryName" : "US",
"ZipCode" : "567334"
},
"id" : "3"
}
}
{
"_id" : ObjectId("5e039ae8f5e889d7a519950c"),
"StudentName" : "Jace",
"Information" : {
"FatherName" : "Bob",
"Place" : {
"CountryName" : "US",
"ZipCode" : "999999"
},
"id" : "4"
}
} 以下は、特定のデータを異なる形式で取得するためのクエリです-
> db.getSpecificData.find({'Information.Place.CountryName':"US"}, {}).limit(2); これにより、次の出力が生成されます-
{ "_id" : ObjectId("5e039abdf5e889d7a5199509"), "StudentName" : "John", "Information" : { "FatherName" : "Chris", "Place" : { "CountryName" : "US", "ZipCode" : "111344" }, "id" : "1" } }
{ "_id" : ObjectId("5e039ae7f5e889d7a519950b"), "StudentName" : "David", "Information" : { "FatherName" : "Carol", "Place" : { "CountryName" : "US", "ZipCode" : "567334" }, "id" : "3" } } -
2つの日付の間で、MongoDBの特定の値を使用してデータをフェッチします。グループ化して、カウントで合計を取得しますか?
一致させるには、MongoDBで$ matchを使用し、2つの日付の間のデータを取得するには、$gteと$lteを使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo560.insertOne({"value1":40,"value2":40,shippingDate:new ISODate("2020-02-26")});{ "acknowledged" : true, "insertedId" : ObjectId("
-
MongoDBドキュメントからemail-idを取得し、print()で表示するにはどうすればよいですか?
このためには、forEach()をprint()と一緒に使用して、email-id値を表示します。ドキュメントを使用してコレクションを作成しましょう- > db.demo690.insertOne({"UserName":"John","UserEmailId":"John@gmail.com"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6