이를 위해 업데이트와 함께 $push를 사용합니다. 문서로 컬렉션을 만들자 −
> db.demo573.insertOne(
... {
... '_id' :101,
... 'SearchInformation' : [
... {
... 'Site' : 'Facebook.com',
... 'NumberOfHits' : 100
... },
... {
... 'Site' : 'Twitter.com',
... 'NumberOfHits' : 300
... }
... ]
... }
... );
{ "acknowledged" : true, "insertedId" : 101 } find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo573.find();
이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : 101, "SearchInformation" : [ { "Site" : "Facebook.com", "NumberOfHits" : 100 }, { "Site" : "Twitter.com", "NumberOfHits" : 300 } ] } 다음은 MongoDB에 포함된 문서 배열을 쿼리하는 방법입니다 -
> db.demo573.update({
... _id: 101,
... "SearchInformation.Site": {
... $nin: ["Google.com"]
... }
... }, {
... $push: {
... "SearchInformation": {
... 'Site' : 'Google.com',
... 'NumberOfHits' : 10000
... }
... }
... });
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo573.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : 101,
"SearchInformation" : [
{
"Site" : "Facebook.com",
"NumberOfHits" : 100
},
{
"Site" : "Twitter.com",
"NumberOfHits" : 300
},
{
"Site" : "Google.com",
"NumberOfHits" : 10000
}
]
}