Convert ObjectID (Mongodb) to String in JavaScript

后端 未结 16 1301
青春惊慌失措
青春惊慌失措 2020-12-02 19:50

I want to convert ObjectID (Mongodb) to String in JavaScript. When I get a Object form MongoDB. it like as a object has: timestamp, second, inc, machine. I can\'t convert to

16条回答
  •  天命终不由人
    2020-12-02 20:25

    Use this simple trick, your-object.$id

    I am getting an array of mongo Ids, here is what I did.

    jquery:

    ...
    success: function (res) {
       console.log('without json res',res);
        //without json res {"success":true,"message":" Record updated.","content":[{"$id":"58f47254b06b24004338ffba"},{"$id":"58f47254b06b24004338ffbb"}],"dbResponse":"ok"}
    
    var obj = $.parseJSON(res);
    
    if(obj.content !==null){
        $.each(obj.content, function(i,v){
            console.log('Id==>', v.$id);
        });
    }
    
    ...
    

提交回复
热议问题