M
M
mozart13372016-04-03 00:28:51
MongoDB
mozart1337, 2016-04-03 00:28:51

Which one to query mongodb?

{
  nick: 'testNick',
  params: [{
    nick2: 'testNick2',
    unique: true
  }, {
    nick2: 'testNick3'
  }]
}

The data structure is like this. Known for nick and nick2. We need to update the params array (specifically the unique parameter) where nick2 is equal to the known nick2. Possible situations:
1. The params array may not contain an array where nick2 is
2. The document may not contain a params array
at all 3. There may not be a document
at all In any of these situations, the following should be created:
{
  nick: 'testNick',
  params: [{
    nick: 'testNick2',
    unique: true
  }]
}

It seems to have explained. Is it possible to do this in one request to the monge?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2016-04-03
@mozart1337

Make such a structure that it is easy and fast to write / read data (if you want it to work efficiently on big data).
For example, in your case:
_id: 'testNick' - gives uniqueness by nick within the collection (and uses the main index on the case, no need to create an additional index - saves memory).
'nick2.testNick2': true - using a dictionary instead of an array gives uniqueness within the document.
Multiple invocation of the command does not create duplicates.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question