Skip to content
Advertisement

MongoDB & PHP | How to add into object that is inside an array?

I am new to MongoDB and PHP and I am trying to make an API. The current problem I have is that I am not able to add an array into an object that is inside an array (of objects). I have tried a lot, but all failed.

This is the document I have so far:

JavaScript

I would like to add an array after domainvalue, but can’t seem to do that.

My code:

JavaScript

The $data variable:

JavaScript

How would I change this code so that there is an array added after domainvalue? I have tried doing things like this:

JavaScript

But without any luck. Can someone please help me because I am really stuck with this problem. Thanks in advance!

Advertisement

Answer

To create a new field for the nested documents in an array, you need the $set operator and $[] all positional operator.

JavaScript

Sample Mongo Playground

While for PHP syntax:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement