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

MongoDBの特定のフィールドのすべての値を一覧表示しますか?


MongoDBの特定のフィールドのすべての値のリストを取得するには、distinct()を使用できます。構文は次のとおりです-

db.yourCollectionName.distinct( "yourFieldName");

上記の構文を理解するために、ドキュメントを使用してコレクションを作成しましょう。ドキュメントを使用してコレクションを作成するためのクエリは次のとおりです-

> db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[10,20,30]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8fc89ed3c9d04998abf011")
}
> db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[40,50,60]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8fc8abd3c9d04998abf012")
}
> db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[10,20,30]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8fc8d7d3c9d04998abf013")
}
> db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[40,50,70]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8fc8e2d3c9d04998abf014")
}

find()メソッドを使用して、コレクションのすべてのドキュメントを表示します。クエリは次のとおりです-

> db.listAllValuesOfCeratinFieldsDemo.find().pretty();

以下は出力です-

{
   "_id" : ObjectId("5c8fc89ed3c9d04998abf011"),
   "ListOfValues" : [
      10,
      20,
      30
   ]
}
{
   "_id" : ObjectId("5c8fc8abd3c9d04998abf012"),
   "ListOfValues" : [
      40,
      50,
      60
   ]
}
{
   "_id" : ObjectId("5c8fc8d7d3c9d04998abf013"),
   "ListOfValues" : [
      10,
      20,
      30
   ]
}
{
   "_id" : ObjectId("5c8fc8e2d3c9d04998abf014"),
   "ListOfValues" : [
      40,
      50,
      70
   ]
}

これは、MongoDBの特定のフィールドのすべての値のリストを取得するためのクエリです。フィールド「ListOfValues」のレコードを表示しています-

> db.listAllValuesOfCeratinFieldsDemo.distinct( "ListOfValues");

以下は出力です-

[ 10, 20, 30, 40, 50, 60, 70 ]

  1. MongoDBの特定の文字列でフィールドのすべての値を更新しますか?

    すべての値を更新するには、update()をmulti:trueとともに使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo720.insertOne({"SubjectName":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eaae7ca43417811278f5883") } > db.demo720.insertOn

  2. MySQLフィールドのすべての値を0に設定しますか?

    フィールドのすべての値を0に設定するには、更新コマンド-を使用します。 update yourTableName set yourColumnName=0;を更新します まずテーブルを作成しましょう- mysql> create table DemoTable    (    Number int    ); Query OK, 0 rows affected (0.64 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTable values