Pravitha V
Pravitha V

Reputation: 3308

Deleting a value from an embedded document in mongoengine

I have a document called login_info which contains the following fields:

class login_info(Document):
    user_name = StringField(max_length=120)
    password = StringField(max_length=120)
    email = EmailField()
    gender = StringField(max_length=120)
    date_of_birth = DateTimeField()
    visibility  = StringField(max_length=120)
    client_id = ObjectIdField(required=False)
    location = ListField(EmbeddedDocumentField("Tracking"))`

In the above fields location is an embedded document which contains the following fields:

I want to delete a value from this embedded document which satisfy the following condition.

time < system_datetime.

following is an example data in the login_info document:

{
    "_cls":"login_info",
    "_id":ObjectId("5046f43c12d0592e3f59e25d"),
    "_types":[
        "login_info"
    ],
    "date_of_birth":ISODate("2011-02-07T00:00:00 Z"),
    "email":"[email protected]",
    "expire":1346827684,
    "gender":"male",
    "issue":1346827324,
    "key":"47d1e64e51dfa1cf99ce4a59e0c940",
    "location":[
        {
            "Latitude":"5615.3111",
            "_types":[
                "Tracking"
            ],
            "_cls":"Tracking",
            "Longitude":"1236.711",
            "time":ISODate("2012-09-13T12:24:36.051 Z")
        },
        {
            "Latitude":"000",
            "_types":[
                "Tracking"
            ],
            "_cls":"Tracking",
            "Longitude":"3.70",
            "time":ISODate("2012-09-25T18:30:57.756 Z")
        },
        {
            "Latitude":"11",
            "_types":[
                "Tracking"
            ],
            "_cls":"Tracking",
            "Longitude":"1",
            "time":ISODate("2012-09-26T10:25:29.157 Z")
        },
        {
            "Latitude":"11",
            "_types":[
                "Tracking"
            ],
            "_cls":"Tracking",
            "Longitude":"1",
            "time":ISODate("2012-09-26T10:40:58.895 Z")
        },
        {
            "Latitude":"11",
            "_types":[
                "Tracking"
            ],
            "_cls":"Tracking",
            "Longitude":"1",
            "time":ISODate("2012-09-26T10:54:08.361 Z")
        },
        {
            "Latitude":"11",
            "_types":[
                "Tracking"
            ],
            "_cls":"Tracking",
            "Longitude":"1",
            "time":ISODate("2012-09-26T11:08:55.873 Z")
        }
    ],
    "password":"jack",
    "refresh_token":"22580a8f69",
    "token":"bac8a5f863",
    "user_name":"jack",
    "visibility":"visible"
}

Upvotes: 1

Views: 1213

Answers (1)

Gianfranco P
Gianfranco P

Reputation: 10794

If you want to remove a single location embedded document from a login_info document with MongoEngine, do something like:

// add 
loc1 = Tracking( time=datetime(2011, 11, 5, 0, 0, 0) )
loc2 = Tracking( time=datetime(2012, 10, 5, 0, 0, 0) )

login = login_info( user_name='Mark', location=[loc1, loc2] )
login.save()

// remove locations from the location list
login.objects( title=user_name='Mark' ).update_one( pull__lt=datetime.now() )

Note: The mongoengine 0.7.5 has a bug, so you'll have to download the latest version from https://github.com/MongoEngine/mongoengine

Upvotes: 3

Related Questions