MongoDB Composite Key query with one field unknown - mongodb

I have a database that has a unique combination of two fields (x and i) for every entry. So I have set the _id field to be {_id: {a: x, b: i}}. Now I want to retrieve all values that have a certain value x but that have any value for i.
Example:
{_id: {a: 1, b: 5}},
{_id: {a: 1, b: 3}},
{_id: {a: 2, b: 5}}
{_id: {a: 3, b: 3}}
Now I want to do something like: db.find({_id: {a: 1, b: { $exists: true}}) or even easier: db.find({_id: {a: 1}) that should return:
{_id: {a: 1, b: 5}},
{_id: {a: 1, b: 3}}
Is there any way I could achieve this? Or in other words can you query in any way on this composite primary key? Currently I added the fields to the object itself but this is not really an optimal solution as my data set gets really large.
Edit:
db.someCollection.find({"_id.a": 1, "_id.b": { $exists: true}})
Seems to be a solution, this is however just as slow as adding a as a field (not in the key) to the object. Is there a faster method?

Have you tried this?
db.someCollection.find({"_id.a": 1, "_id.b": { $exists: true}})

Related

Is possible to create an index on a field that holds an array of subdocuments?

{
field_1: [
{A: 1, ...}
{A: 10, ...}
{A: 2, ...}
]
}
The goal is to create an index on field_1.A, so that when the collection is $unwind to field_1, then field_1.A can be used as an index.

Mongo remove document if all elements in array match condition

Docs:
{
_id: 1,
items: [{thing: 5}, {thing: 7}]
}
{
_id: 2,
items: [{thing: 5}, {thing: 11}]
}
I would like to remove all docs from the collection above if all elements in array have "thing" < 10. IE for that case doc 1 should be removed, doc 2 should remain.
Is it possible with a query to find only docs where all elements in the array match with a $lt query?
I tried this:
db.mycollection.remove({items: {$all: [{$elemMatch: {thing: {$lt: 11}}}]}})
However that will remove all docs if any of the elements in the array match the condition.
Use double negative (De-Morgan law):
{items: {$not: {$elemMatch: {thing: {$gte: 11}}}}}

How to create index to process $in queries sorted by another field?

Is there any way to create index to process queries like
db.col.find({a: {$in: [1, 2, 3]}).sort({b: 1})
or
db.col.find({a: {$in: [1, 2, 3]}).sort({b: 1}).limit(N)
effectively?
It's said here that index {a: 1, b: 1} should be used with SORT_MERGE for such queries but it doesn't, explain() gives me regular SORT only. This index is used only if I have query like db.col.find({a: 1}).sort({b: 1}) or db.col.find({a: {$in: [1]}).sort({b: 1}) (with only one $in element).
hint({a:1, b:1}) also did not help.
Upd. Mongodb 3.2.1

MongoDB : Aggregate and Match duplicated values in two fields

I have the collection with the following data in in
{_id: 1, a : 12345, b : [2,3]},
{_id: 2, a : 666},
{_id: 3, a : 6, b : [15, 20]},
{_id: 4, a : 1111, b : [12345, 156]}
I need to match same values that are present in a and b. For the following data the aggregation should return 12345.
I use the next code to get all values from b
db.collection.aggregate([
{$unwind: "$b"},
{$project: {_id: "$b"}}
])
But stuck on what to do next and how to compare results with a field.
Ty for your help.

MongoDb - How can I update multiple elements of a nested object using $set?

Lets say I have the following document:
{name: 'myDoc', nestedDoc: {a: 1, b: 2, c: 3}}
And I would like to merge with the nestedDoc a new object:
{b: 20, c:30, d:40}
So the resulting object would be:
{name: 'myDoc', nestedDoc: {a: 1, b: 20, c: 30, d: 40}}
How can I go about doing this in a single query? I feel like I need multiple $set calls however object property names must be unique. In other words, I wish I could do the following:
db.myCollection.update({name: 'myDoc', nestedDoc: {$set: {b: 20}, $set: {c: 30}, $set: {d: 40}});
Some extra details are that the MongoDB version is 1.8.2 and I am using the NodeJS node-native driver.
You can update by using the following:
db.myCollection.update({
name: 'mydoc'
}, {
$set: {
'nestedDoc.b': 20,
'nestedDoc.c': 30,
'nestedDoc.d': 40
}
})
Here is more information about update command:
Update Documents
update: this answer is not a correct update!
this works too in my app, and easy to read
db.myCollection.update({
name: 'mydoc'
},
{
$set: {
nestedDoc:{
b: 20,
c: 30,
d: 40,
}
}
})