MongoDB
 Computer >> コンピューター >  >> プログラミング >> MongoDB

MongoDBドキュメントからemail-idを取得し、print()で表示するにはどうすればよいですか?


このためには、forEach()をprint()と一緒に使用して、email-id値を表示します。ドキュメントを使用してコレクションを作成しましょう-

> db.demo690.insertOne({"UserName":"John","UserEmailId":"[email protected]"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea6db31551299a9f98c939c")
}
> db.demo690.insertOne({"UserName":"Bob","UserEmailId":"[email protected]"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea6db3c551299a9f98c939d")
}
> db.demo690.insertOne({"UserName":"David","UserEmailId":"[email protected]"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea6db47551299a9f98c939e")
}
>

find()メソッドを使用してコレクションからすべてのドキュメントを表示する-

> db.demo690.find();

これにより、次の出力が生成されます-

{ "_id" : ObjectId("5ea6db31551299a9f98c939c"), "UserName" : "John", "UserEmailId" : "[email protected]" }
{ "_id" : ObjectId("5ea6db3c551299a9f98c939d"), "UserName" : "Bob", "UserEmailId" : "[email protected]" }
{ "_id" : ObjectId("5ea6db47551299a9f98c939e"), "UserName" : "David", "UserEmailId" : "[email protected]" }

以下は、MongoDBドキュメントからemail-idを取得し、print()-

を使用して印刷するためのクエリです。
> db.demo690.find().forEach(function(document) {
...    print(document.UserEmailId);
... });

これにより、次の出力が生成されます-

[email protected]
[email protected]
[email protected]

  1. ユーザー入力から番号を取得し、JavaScriptを使用してコンソールに表示します

    #を使用して、ユーザーがdocument.querySelector(“”);を使用してボタンをクリックしたときに値を取得できます。以下はJavaScriptコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <titl

  2. trタグからidを取得し、JavaScriptを使用して新しいtdに表示するにはどうすればよいですか?

    以下が私たちのテーブルだとしましょう- <table>    <tr id='StudentDetails'>       <th>StudentName</th>       <th>StudentCountryName</th>    </tr>    <tr id='FirstRow'>       <td&