데이터는 이렇게 들어가있음.
{ "_id" : ObjectId("4qcf2a857d9f8ac73bf43c94"), ...... blabla... "type" : "youtube", "youtube" : { "items" : [ { "_id" : ObjectId("4qcf24457d9f8ac73bf43c8e") }, { "_id" : ObjectId("4qcf244d7d9f8ac73bf43c8f") }, { "_id" : ObjectId("4qcf24707d9f8ac73bf43c91") }, { "_id" : ObjectId("4qcf2a457d9f8ac73bf43c93") }, { "_id" : ObjectId("4qcf24c37d9f8ac73bf43c92") } ], "outlink" : false } } |
find({"youtube.items":{"_id" : ObjectId("4qcf2a457d9f8ac73bf43c93")}})
> youtube.items array에 특정 엘리먼트값을 조회한다.
$pull
> youtube.items array에 특정 엘리먼트값을 제거한다.
mongos> db.collection_name.find({"type":"youtube", "youtube.items":{"_id" : ObjectId("4qcf2a457d9f8ac73bf43c93")}}).pretty()
{
"_id" : ObjectId("4qcf2a857d9f8ac73bf43c94"),
"type" : "youtube",
"youtube" : {
"items" : [
{
"_id" : ObjectId("4qcf24457d9f8ac73bf43c8e")
},
{
"_id" : ObjectId("4qcf244d7d9f8ac73bf43c8f")
},
{
"_id" : ObjectId("4qcf24557d9f8ac73bf43c90")
},
{
"_id" : ObjectId("4qcf24707d9f8ac73bf43c91")
},
{
"_id" : ObjectId("4qcf2a457d9f8ac73bf43c93")
},
{
"_id" : ObjectId("4qcf24c37d9f8ac73bf43c92")
}
],
"outlink" : false
},
"created_at" : 1523526277.579,
"created_by" : ObjectId("379583f6b5ebd63f34788c1f"),
"updated_at" : 1523953087.116,
"updated_by" : ObjectId("279583f6b5ebd63f34788c3f")
}
update 쿼리
mongos> db.collection_name.update({"_id" : ObjectId("4acf2a857d9f8ac73bf43c94")},{"$pull":{"youtube.items":{"_id" : ObjectId("4acf24557d9f8ac73bf43c90")}}})
update가 잘 됐는지 볼까?
mongos> db.section_contents.find({"_id" : ObjectId("4acf2a857d9f8ac73bf43c94")}).pretty()
{
"_id" : ObjectId("5acf2a857d9f8ac73bf43c94"),
......
"youtube" : {
"items" : [
{
"_id" : ObjectId("4acf24457d9f8ac73bf43c8e")
},
{
"_id" : ObjectId("4acf244d7d9f8ac73bf43c8f")
},
{
"_id" : ObjectId("4acf24707d9f8ac73bf43c91")
},
{
"_id" : ObjectId("4acf2a457d9f8ac73bf43c93")
},
{
"_id" : ObjectId("4acf24c37d9f8ac73bf43c92")
}
],
"outlink" : false
}
}
문서 : https://docs.mongodb.com/v2.4/reference/operator/update/pull/index.html
'데이터베이스' 카테고리의 다른 글
docker mongodump and mongorestore (0) | 2019.08.19 |
---|---|
mongodump and mongorestore (0) | 2019.08.19 |
mongo mongo_slave_server_host:port database < file.js > dump_users_`date +%Y%m%d`.log (0) | 2018.07.03 |
mongo run javascript file (0) | 2018.05.24 |
mongoexport / mongoimport 하기 (0) | 2018.05.16 |