Computer >> 컴퓨터 >  >> 프로그램 작성 >> MongoDB

특정 문자열로 MongoDB 문서 추출

<시간/>

특정 문자열로 MongoDB 문서를 추출하려면 MongoDB에서 $match를 사용합니다. 문서로 컬렉션을 만들자 −

> db.demo424.insert(
...    {
...
...       "Information" : [
...          {
...             id:10,
...             Name:"Chris"
...          },
...          {
...             id:11,
...             Name:"David"
...          }
...       ]
...    }
... )
WriteResult({ "nInserted" : 1 })
>
> db.demo424.insert(
...    {
...
...       "Information" : [
...          {
...             id:101,
...             Name:"Mike"
...          },
...          {
...             id:102,
...             Name:"John"
...          }
...       ]
...    }
... )
WriteResult({ "nInserted" : 1 })

find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -

> db.demo424.find();

이것은 다음과 같은 출력을 생성합니다 -

{ "_id" : ObjectId("5e74eb9dbbc41e36cc3cae6a"), "Information" : [ { "id" : 10, "Name" : "Chris" }, { "id" : 11, "Name" : "David" } ] }
{ "_id" : ObjectId("5e74eb9ebbc41e36cc3cae6b"), "Information" : [ { "id" : 101, "Name" : "Mike" }, { "id" : 102, "Name" : "John" } ] }

다음은 특정 문자열로 MongoDB 문서를 추출하는 쿼리입니다 -

> db.demo424.aggregate(
... [ { $match : { "Information.Name": "Chris" } } ]
... );

이것은 다음과 같은 출력을 생성합니다 -

{ "_id" : ObjectId("5e74eb9dbbc41e36cc3cae6a"), "Information" : [ { "id" : 10, "Name" : "Chris" }, { "id" : 11, "Name" : "David" } ] }