Source: https://stackoverflow.com/questions/24294273/mongodb-query-a-json-object-nested-in-an-array
mongodb: Query a json-object nested in an array
5
3
|
I'm quite new to mongodb and there is one thing I can't solve right now:
Let's pretend, you have the following document (simplified):
Which query would return the json-object, in which the value equals 'value2'?
That means, i need this json-object:
Of course I already tried a lot of possible queries, but none of them returned the right, e.g.
Can someone help and show me, what I'm doing wrong?
Thanks! | |||
add a comment
|
10
|
Using Positional operator
Output
Using aggregation
output
Using Java Driver
output
|
1
|
Try the $in operator like this:
db.test.find({"array.value" : { $in : ["value2"]}}) | ||||
|
0
|
use $elemMatch and dot(.) to get your required output
|
- Introduction to MongoDB >
- MongoDB Extended JSON
MongoDB Extended JSON
JSON can only represent a subset of the types supported by BSON. To preserve type information, MongoDB adds the following extensions to the JSON format:
- Strict mode. Strict mode representations of BSON types conform to the JSON RFC. Any JSON parser can parse these strict mode representations as key/value pairs; however, only the MongoDB internal JSON parser recognizes the type information conveyed by the format.
mongoShell mode. The MongoDB internal JSON parser and themongoshell can parse this mode.
The representation used for the various data types depends on the context in which the JSON is parsed.
Parsers and Supported Format
Input in Strict Mode
The following can parse representations in strict mode with recognition of the type information.
- REST Interfaces
mongoimport--queryoption of various MongoDB tools- MongoDB Compass
Input in mongo Shell Mode
The following can parse representations in
mongo shell mode with recognition of the type information.- REST Interfaces
mongoimport--queryoption of various MongoDB toolsmongoshell
Output in Strict mode
mongoexport and REST and HTTP Interfaces output data in Strict mode.
Output in mongo Shell Mode
bsondump outputs in mongo Shell mode.BSON Data Types and Associated Representations
The following presents the BSON data types and the associated representations in Strict mode and
mongoShell mode.Binary
data_binaryStrict Mode mongoShell Mode{ "$binary": "<bindata>", "$type": "<t>" }BinData ( <t>, <bindata> )
<bindata>is the base64 representation of a binary string.<t>is a representation of a single byte indicating the data type. In Strict mode it is a hexadecimal string, and in Shell mode it is an integer. See the extended bson documentation. http://bsonspec.org/spec.html
Date
data_dateStrict Mode mongoShell Mode{ "$date": "<date>" }new Date ( <date> )
In Strict mode,<date>is an ISO-8601 date format with a mandatory time zone field following the templateYYYY-MM-DDTHH:mm:ss.mmm<+/-Offset>.The MongoDB JSON parser currently does not support loading ISO-8601 strings representing dates prior to the Unix epoch. When formatting pre-epoch dates and dates past what your system’stime_ttype can hold, the following format is used:{ "$date" : { "$numberLong" : "<dateAsMilliseconds>" } }In Shell mode,<date>is the JSON representation of a 64-bit signed integer giving the number of milliseconds since epoch UTC.
Timestamp
data_timestampStrict Mode mongoShell Mode{ "$timestamp": { "t": <t>, "i": <i> } }Timestamp( <t>, <i> )
<t>is the JSON representation of a 32-bit unsigned integer for seconds since epoch.<i>is a 32-bit unsigned integer for the increment.
Regular Expression
data_regexStrict Mode mongoShell Mode{ "$regex": "<sRegex>", "$options": "<sOptions>" }/<jRegex>/<jOptions>
<sRegex>is a string of valid JSON characters.<jRegex>is a string that may contain valid JSON characters and unescaped double quote (") characters, but may not contain unescaped forward slash (/) characters.<sOptions>is a string containing the regex options represented by the letters of the alphabet.<jOptions>is a string that may contain only the characters ‘g’, ‘i’, ‘m’ and ‘s’ (added in v1.9). Because theJavaScriptandmongo Shellrepresentations support a limited range of options, any nonconforming options will be dropped when converting to this representation.
OID
data_oidStrict Mode mongoShell Mode{ "$oid": "<id>" }ObjectId( "<id>" )
<id>is a 24-character hexadecimal string.
DB Reference
data_refStrict Mode mongoShell Mode{ "$ref": "<name>", "$id": "<id>" }DBRef("<name>", "<id>")
<name>is a string of valid JSON characters.<id>is any valid extended JSON type.
Undefined Type
data_undefinedStrict Mode mongoShell Mode{ "$undefined": true }undefined
The representation for the JavaScript/BSON undefined type.You cannot useundefinedin query documents. Consider the following document inserted into thepeoplecollection:db.people.insert( { name : "Sally", age : undefined } )
The following queries return an error:db.people.find( { age : undefined } ) db.people.find( { age : { $gte : undefined } } )
However, you can query for undefined values using$type, as in:db.people.find( { age : { $type : 6 } } )
This query returns all documents for which theagefield has valueundefined.
MinKey
data_minkeyStrict Mode mongoShell Mode{ "$minKey": 1 }MinKey
The representation of the MinKey BSON data type that compares lower than all other types. SeeComparison/Sort Order for more information on comparison order for BSON types.
MaxKey
data_maxkeyStrict Mode mongoShell Mode{ "$maxKey": 1 }MaxKey
The representation of the MaxKey BSON data type that compares higher than all other types. SeeComparison/Sort Order for more information on comparison order for BSON types.
NumberLong
New in version 2.6.
data_numberlongStrict Mode mongoShell Mode{ "$numberLong": "<number>" }NumberLong( "<number>" )
NumberLongis a 64 bit signed integer. You must include quotation marks or it will be interpreted as a floating point number, resulting in a loss of accuracy.For example, the following commands insert9223372036854775807as aNumberLongwith and without quotation marks around the integer value:db.json.insert( { longQuoted : NumberLong("9223372036854775807") } ) db.json.insert( { longUnQuoted : NumberLong(9223372036854775807) } )
When you retrieve the documents, the value oflongUnQuotedhas changed, whilelongQuotedretains its accuracy:db.json.find() { "_id" : ObjectId("54ee1f2d33335326d70987df"), "longQuoted" : NumberLong("9223372036854775807") } { "_id" : ObjectId("54ee1f7433335326d70987e0"), "longUnQuoted" : NumberLong("-9223372036854775808") }
NumberDecimal
New in version 3.4.
data_numberdecimalStrict Mode mongoShell Mode{ "$numberDecimal": "<number>" }NumberDecimal( "<number>" )
NumberDecimalis a high-precision decimal. You must include quotation marks, or the input number will be treated as a double, resulting in data loss.For example, the following commands insert123.40as aNumberDecimalwith and without quotation marks around the value:db.json.insert( { decimalQuoted : NumberDecimal("123.40") } ) db.json.insert( { decimalUnQuoted : NumberDecimal(123.40) } )
When you retrieve the documents, the value ofdecimalUnQuotedhas changed, whiledecimalQuotedretains its specified precision:db.json.find() { "_id" : ObjectId("596f88b7b613bb04f80a1ea9"), "decimalQuoted" : NumberDecimal("123.40") } { "_id" : ObjectId("596f88c9b613bb04f80a1eaa"), "decimalUnQuoted" : NumberDecimal("123.400000000000") }
Was this page helpful?

