1

Надо добавить в массив requests, который находиться в объекте friends объект request, но я не понимаю как. Как бы не пытался - ругается.

request = {
    "name": friend.name,
    "id": friend.id,
    "type": "outbox"
  };
  db.collection('users').updateOne({token: token}, {$push: {friends.requests: request}});

1 ответ 1

1

Так а на что ругается то? Я вижу у тебя friends.requests не как стгринга. Попробуй так:

db.collection('users').updateOne({token: token}, {$push: {"friends.requests": request}});

Вот мой вариант, все работает отлично:

import {MongoClient} from 'mongodb'

;(async function main(){
    const client = await MongoClient.connect("mongodb://localhost:27017")
    const db = client.db("someDB")
    const collection = db.collection("someCollection")

    await collection.insertOne({token: 123, friends: {requests: []}})

    const request = {
        "name": "some name",
        "id": "some id",
        "type": "outbox"
    };
    await collection.updateOne({token: 123}, {$push: {'friends.requests': request}});

    const results = await collection.find({}).toArray()
    console.log(JSON.stringify(results, null, 2))

    client.close()
})()

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.