MongoDBのネストされたオブジェクト構造からオブジェクトにアクセスします
ドット表記を使用してオブジェクトにアクセスします。まず、ドキュメントを使用してコレクションを作成しましょう
> db.nestedObjectDemo.insertOne({"Student" : { "StudentDetails" : { "StudentPersonalDetails" : { "StudentName" : [ "John" ],
... "StudentCountryName" : [ "US" ],
... "StudentCoreSubject" : [ "C", "Java" ],
... "StudentProject" : [ "Online Book Store", "Pig Dice Game" ] } } } });
{
"acknowledged" : true,
"insertedId" : ObjectId("5c99dfc2863d6ffd454bb650")
} 以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです
> db.nestedObjectDemo.find().pretty();
これにより、次の出力が生成されます
{
"_id" : ObjectId("5c99dfc2863d6ffd454bb650"),
"Student" : {
"StudentDetails" : {
"StudentPersonalDetails" : {
"StudentName" : [
"John"
],
"StudentCountryName" : [
"US"
],
"StudentCoreSubject" : [
"C",
"Java"
],
"StudentProject" : [
"Online Book Store",
"Pig Dice Game"
]
}
}
}
} 以下は、ドット表記を使用してネストされたオブジェクトにアクセスするためのクエリです
>db.nestedObjectDemo.find({"Student.StudentDetails.StudentPersonalDetails.StudentName":"John"}).pretty(); これにより、次の出力が生成されます
{
"_id" : ObjectId("5c99dfc2863d6ffd454bb650"),
"Student" : {
"StudentDetails" : {
"StudentPersonalDetails" : {
"StudentName" : [
"John"
],
"StudentCountryName" : [
"US"
],
"StudentCoreSubject" : [
"C",
"Java"
],
"StudentProject" : [
"Online Book Store",
"Pig Dice Game"
]
}
}
}
} -
ネストされたMongoDBドキュメントのキーのみを表示するにはどうすればよいですか?
ドキュメントを使用してコレクションを作成しましょう- > db.demo740.insertOne({ ... "details": ... [ ... { ... Name:"Chris", ... Age:21, ... CountryName:&qu
-
JavaでJsonNodeのJSONフィールド、配列、ネストされたオブジェクトにアクセスするにはどうすればよいですか?
JsonNode ジャクソンのツリーモデルです JSONの場合、JSONをJsonNodeインスタンスに読み込み、JsonNodeをJSONに書き出すことができます。 ObjectMapper を作成して、Jacksonを使用してJSONをJsonNodeに読み込むには インスタンスを作成し、 readValue()を呼び出します 方法。 フィールド、配列にアクセスできます またはネストされたオブジェクト get()を使用する JsonNodeのメソッド クラス。 asText()を使用して、有効な文字列表現を返すことができます メソッドを実行し、ノードの値を Java intに変換