首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在mongodb中如何在不干扰外部数据的情况下从集合中删除数据

在mongodb中如何在不干扰外部数据的情况下从集合中删除数据
EN

Stack Overflow用户
提问于 2019-11-20 19:12:35
回答 2查看 61关注 0票数 0

在这里,我试图获取整个数据,但如果日期小于当前日期,则不要从数据库中获取该日期。

代码语言:javascript
复制
{ 
"_id" : ObjectId("5d6fad0f9e0dc027fc6b5ab5"), 
"highlights" : [
    "highlights-1", 

], 
"notes" : [
    "Listen"
], 
"soldout" : false, 
"active" : false, 
"operator" : ObjectId(""), 
"title" : "2D1N Awesome trip to Knowhere 99", 
"destinations" : [
    {
        "coordinatesType" : "Point", 
        "_id" : ObjectId("5d6fad0f9e0dc027fc6b5ab6"), 

    }
], 
"difficulty" : "Easy", 
"duration" : {
    "_id" : ObjectId("5d6fad0f9e0dc027fc6b5ab7"), 
    "days" : NumberInt(2), 
    "nights" : NumberInt(1)
}, 
"media" : {
    "_id" : ObjectId("5d6fad0f9e0dc027fc6b5ab8"), 
    "images" : [

    ], 
    "videos" : [

    ]
}, 
"description" : "Surrounded ", 
"inclusions" : [
    {
        "_id" : ObjectId(""), 
        "text" : "Included"
    }
], 
"itinerary" : "Surrounded .", 
"thingsToCarry" : [
    {
        "_id" : ObjectId(""), 
        "text" : "Yourself"
    }
], 
"exclusions" : [
    {
        "_id" : ObjectId(""), 
        "text" : "A Lot"
    }
], 
"policy" : "Fully refundable 7777 Days before the date of Experience", 
"departures" : [
    {
        "dates" : [
            ISODate("2019-11-19T02:44:58.989+0000"), 
            ISODate("2019-11-23T17:19:47.878+0000")
        ], 
        "_id" : ObjectId(""), 
        "bookingCloses" : "2 Hours Before", 
        "maximumSeats" : NumberInt(20), 
        "source" : {
            "coordinatesType" : "Point", 
            "_id" : ObjectId("5d6fad0f9e0dc027fc6b5ac2"), 
            "code" : "code", 
            "name" : "Manali", 
            "state" : "Himachal Pradesh", 
            "region" : "North", 
            "country" : "India", 
            "coordinates" : [
                23.33, 
                NumberInt(43), 
                NumberInt(33)
            ]
        }, 
        "pickupPoints" : [
            {
                "coordinatesType" : "Point", 
                "_id" : ObjectId("5d6fad0f9e0dc027fc6b5ac3"), 
                "name" : "name-3", 
                "address" : "address-3", 
                "time" : "time-3", 
                "coordinates" : [
                    23.33, 
                    NumberInt(43), 
                    NumberInt(33)
                ]
            }
        ], 
        "prices" : {
            "3" : NumberInt(5)
        }, 
        "mrps" : {
            "3" : NumberInt(5)
        }, 
        "markup" : NumberInt(25), 
        "discount" : NumberInt(0), 
        "b2m" : {
            "3" : NumberInt(5)
        }, 
        "m2c" : {
            "3" : 6.25
        }, 
        "minimumOccupancy" : NumberInt(3), 
        "maximumOccupancy" : NumberInt(3)
    }
], 
"bulkDiscounts" : [
    {
        "_id" : ObjectId("5d6fad0f9e0dc027fc6b5ac4")
    }
],     
}

在这方面,我试图获得所有的数据,除了日期部分应该是不同的。意味着我应该得到如下输出

代码语言:javascript
复制
{
            "_id": "5d6fad0f9e0dc027fc6b5ab5",
            "highlights": [
                "highlights-1",
                "highlights-2",
                "highlights-3",
                "highlights-4",
                "highlights-5"
            ],
            "notes": [
                "Listen"
            ],
            "soldout": false,
            "active": false,
            "operator": "5d5d84e8c89fbf00063095f6",
            "title": "2D1N Awesome trip to Knowhere 99",
            "destinations": [
                {
                    "code": "code",
                    "name": "Manali",
                    "coordinates": [
                        23.33,
                        43,
                        33
                    ]
                }
            ],
            "difficulty": "Easy",
            "duration": {
                "_id": "5d6fad0f9e0dc027fc6b5ab7",
                "days": 2,
                "nights": 1
            },
            "media": {
                "_id": "5d6fad0f9e0dc027fc6b5ab8",
                "images": [


            ],
            "videos": []
        },
        "description": "Surrounded.",
        "inclusions": [
            {
                "_id": "5d6fad0f9e0dc027fc6b5abe",
                "text": "Included"
            }
        ],
        "itinerary": "Surrounded",
        "thingsToCarry": [
            {
                "_id": "5d6fad0f9e0dc027fc6b5abf",
                "text": "Yourself"
            }
        ],
        "exclusions": [
            {
                "_id": "5d6fad0f9e0dc027fc6b5ac0",
                "text": "A Lot"
            }
        ],
        "policy": "Fully refundable 7777 Days before the date of Experience",
        "departures": [
            {
                "dates": [
                    "2019-11-23T17:19:47.878Z"
                ],
                "_id": "5d6fad0f9e0dc027fc6b5ac1",
                "bookingCloses": "2 Hours Before",
                "maximumSeats": 20,
                "source": {
                    "code": "code",
                    "name": "Manali",
                    "coordinates": [
                        23.33,
                        43,
                        33
                    ]
                },
                "pickupPoints": [
                    {
                        "coordinatesType": "Point",
                        "_id": "5d6fad0f9e0dc027fc6b5ac3",
                        "name": "name-3",
                        "address": "address-3",
                        "time": "time-3",
                        "coordinates": [
                            23.33,
                            43,
                            33
                        ]
                    }
                ],
                "mrps": {
                    "3": 5
                },
                "markup": 25,
                "discount": 0,
                "b2m": {
                    "3": 5
                },
                "m2c": {
                    "3": 6.25
                },
                "minimumOccupancy": 3,
                "maximumOccupancy": 3
            }
        ],
        "bulkDiscounts": [
            {
                "_id": "5d6fad0f9e0dc027fc6b5ac4"
            }
        ],
        "url": "",


    }
]

我的意思是说,除了日期数组之外,输出没有区别。如果日期小于当前日期,则不需要从带有过滤日期数组的数据库中提取其他日期。

EN

回答 2

Stack Overflow用户

发布于 2019-11-20 19:54:48

如果您使用$addFields 3.4>,那么可以尝试使用mongo和$filter

代码语言:javascript
复制
myCollection.aggregate([
    {$match: { 
      'departures.dates': {
          $elemMatch: {$gt: new Date()}}
       }
    },
    {$addFields: {
        'departures.dates': {
            $filter: {
                input: '$departures.dates',
                as: 'date',
                cond: {
                    $gt: ['$$date', new Date()]
                }
            }
        }
    }}
])
票数 1
EN

Stack Overflow用户

发布于 2019-11-21 01:07:41

我在这里遗漏了一个术语,我的文档结构如下

代码语言:javascript
复制
{
  _id: ObjecId(),
  departure: [{
    dates: [Array]
  }]
}

因此,下面是我在以下代码中的解决方案

代码语言:javascript
复制
pipeline = [
    { $unwind: '$departures' },
    {
        $addFields: {
            'departures.dates': {
                $filter: {
                    input: '$departures.dates',
                    as: 'date',
                    cond: {
                        $gt: ['$$date', new Date()]
                    }
                }
            }
        }
    }
];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58952997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档