Remove items from array of documents in Spring+Mongo

后端 未结 1 643
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-24 22:28

I have a collection of documents like this in a mongo db :

\"_id\" : ObjectId(\"592bc37c339e7a23788b4c7c\"),
\"trips\" : [ 
    {
        \"tripGcsId\" : \"5937f         


        
1条回答
  •  别那么骄傲
    2021-01-24 23:12

    You need to use $pull update operator which takes the query to match and delete all the matching rows in embedded array.

    Something like

    public List removeTripObject( List tripIds ) {
        Query query = Query.query( Criteria.where( "tripGcsId" ).in( tripIds ) );
        Update update = new Update().pull("trips", query );
        getMongoTemplate().updateMulti( new Query(), update, "ORDER" );
        return updatedOrders;
    }
    

    Reference

    https://docs.mongodb.com/manual/reference/operator/update/pull/#remove-items-from-an-array-of-documents

    0 讨论(0)
提交回复
热议问题