범위 내의 배열에서 값을 찾으려면 $gt 및 $lt를 사용합니다. 문서로 컬렉션을 만들자 −
> db.demo341.insertOne({
... "Name": "Chris",
... "productDetails" : [
... {
... "ProductPrice" : {
... "Price" : 800
... }
... },
... {
... "ProductPrice" : {
... "Price" : 400
... }
... },
... {
... "ProductPrice" : {
... "Price" : 300
... }
... }
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e53ed5cf8647eb59e5620a7")
}
> db.demo341.insertOne({
... "Name": "Chris",
... "productDetails" : [
... {
... "ProductPrice" : {
... "Price" : 1000
... }
... },
... {
... "ProductPrice" : {
... "Price" : 1200
... }
... },
... {
... "ProductPrice" : {
... "Price" : 1300
... }
... }
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e53edc1f8647eb59e5620a8")
} find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo341.find();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5e53ed5cf8647eb59e5620a7"), "Name" : "Chris", "productDetails" : [
{ "ProductPrice" : { "Price" : 800 } }, { "ProductPrice" : { "Price" : 400 } },
{ "ProductPrice" : { "Price" : 300 } }
]
}
{
"_id" : ObjectId("5e53edc1f8647eb59e5620a8"), "Name" : "Chris", "productDetails" : [
{ "ProductPrice" : { "Price" : 1000 } }, { "ProductPrice" : { "Price" : 1200 } },
{ "ProductPrice" : { "Price" : 1300 } }
]
} 다음은 여러 기준으로 배열에서 값을 찾는 쿼리입니다 -
> db.demo341.aggregate([
... { "$match": {
... "productDetails": {
... "$elemMatch": {
... "ProductPrice.Price": {
... "$gt": 600,
... "$lt": 900
... }
... }
... }
... }}
... ]).pretty(); 이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5e53ed5cf8647eb59e5620a7"),
"Name" : "Chris",
"productDetails" : [
{
"ProductPrice" : {
"Price" : 800
}
},
{
"ProductPrice" : {
"Price" : 400
}
},
{
"ProductPrice" : {
"Price" : 300
}
}
]
}