Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
712 views
in Technique[技术] by (71.8m points)

store into multiple document with single object data using express and mongodb

{
"token": "ayuAaCahms6qTD8LJrL6e/IaUBZSzAfIg7pOu5zKbkw=", 
 "photo": [{"height": 500, "preview": "xxxx ", "url": "http://xyz.jpg", "width": 375}, {"height": 500, "preview": "skjdfbsdjvns;abc ", "url": "http://abc.jpg", "width": 375}],
 "user_id": "5fce2865cf6b771054133984",
 "mimetype" : "images/jpeg",
 "device_platform" : "ios"
}

How do store this object into multiple document into databases since photo has array. If photo has more than 1 data it should store in mulitple document i.e

{
"_id" : ObjectId("5fce2865cf6b771054133911")
"token": "ayuAaCahms6qTD8LJrL6e/IaUBZSzAfIg7pOu5zKbkw=", 
 "photo": {"height": 500, "preview": "xxxx ", "url": "http://xyz.jpg", "width": 375},
 "user_id": "5fce2865cf6b771054133984",
 "mimetype" : "images/jpeg",
 "device_platform" : "ios"
}

{
"_id" : ObjectId("5fce2865cf6b771054133998")
"token": "ayuAaCahms6qTD8LJrL6e/IaUBZSzAfIg7pOu5zKbkw=", 
 "photo":  {"height": 500, "preview": "skjdfbsdjvns;abc ", "url": "http://abc.jpg", "width": 375},
 "user_id": "5fce2865cf6b771054133984",
 "mimetype" : "images/jpeg",
 "device_platform" : "ios"
}

I tried that way but I am that is not going to work

if(photo !== undefined && photo.length > 0) {
 
      let value = { ...req.body }
      for(let key in { ...value }) {
        let userStatus = []
        if( value.hasOwnProperty(key) ) {
          let record = await new User({
                user_id: value[key].user_id,
                photo: value[key].photo,
                mimetype: value[key].mimetype,
                device_platform: value[key].device_platform,
            }).save()
            userStatus.push(record)            
        }
            

        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...