I'm trying to build a Query that excludes results from an Array when it only contains the String sent in the Query.
I have these two Arrays
Array 1
[ { _id :'first', subCategory : '5K'}, { _id : 'second', subCategory : '10K'}]
Array 2
[ { _id :'first', subCategory : '5K'}]
I want to exclude JUST the second one, so I can't use
query.$and.push({ subCategory: { $nin: '5K }});
I tried this, but it didn't work either :
query.$and.push({ subCategory : { $elemMatch : { $nin : '5K' }}});
Thank you for your time!!
Ok, I solved it.
query.$and.push({ array : { $all : [{ $elemMatch : { subCategory : { $nin : '5K' }}}]}});
Related
I have this collection:
[{ "_id" : 7,
"category" : "Festival",
"comments" : [
{
"_id" : ObjectId("4da4e7d1590295d4eb81c0c7"),
"usr" : "Mila",
"txt" : "This is a comment",
"date" : "4/12/11"
}
]
}]
All I want is to push insert a new field inside comments like this:
[{ "_id" : 7,
"category" : "Festival",
"comments" : [
{
"_id" : ObjectId("4da4e7d1590295d4eb81c0c7"),
"usr" : "Mila",
"txt" : "This is a comment",
"date" : "4/12/11",
"type": "abc" // find the parent doc with id=7 & insert this inside comments
}
]
}]
How can I insert inside the comments subdocument?
You need to use the $ positional operator
For example:
update({
_id: 7,
"comments._id": ObjectId("4da4e7d1590295d4eb81c0c7")
},{
$set: {"comments.$.type": abc}
}, false, true
);
I didn't test it but i hope that it will be helpful for you.
If you want to change the structure of document you need to use
db.collection.update( criteria,
objNew, upsert, multi )
Arguments:
criteria - query which selects the record to update;
objNew - updated object or $ operators (e.g., $inc) which manipulate the object
upsert - if this should be an "upsert"; that is, if the record does not exist, nsert it
multi - if all documents matching criteria should be updated
and insert new objNew with new structure. check this for more details
The $ positional operator is only going to work as expected if the 'comments' field is NOT an array. The OP's json is malformed, but it looks like it could be an array.
The issue is that mongodb right now will only update the first element of an array which matches the query. Though there is an RFE open to add support for updating all matching array elements: https://jira.mongodb.org/browse/SERVER-1243
To work around this issue with arrays you just have to do a regular find then update the elements in the array individually.
I have document in witch has field "leader". Leader can be type string or array. For example {leader: "id"} or {leader: ["id"]}. How I can get all document where leader = id; If I use operator $all, I search in array, if use operator $or I search in field(string). How I can to connect them ? I don't want to do two request. Field has differents type because was had changed structure. Thanks.
"selector": {
"leaderId": {
"$all": ["id"]
}
}
Try this
db.collection.find({$or : [{"leader" : "id"}, {"leader" : { $all : ["id"] }}]})
$set is overtiring complete array value how to update it
var obj = // contains some other data to update as well
obj.images=images; // updating obj with images []
Units.update({_id: 'id', {$set: obj});
finally my mongo object must be something like this
{
"_id" : "ZhBgrNFtr2LRPkQrZ",
"sitouts" : "test",
"interiors" : "test",
"features" : "test",
"keyHolder" : "test",
"images" : [
"1445481257465-8678-20130520_113154.jpg", ## `updated value ` ##
"1445481257456-3-20130520_113157.jpg",
"1445481258058-5771-20130520_113202.jpg",
"1445481258230-9603-20130521_075648.jpg"
]
}
https://docs.mongodb.org/manual/reference/operator/update/push/
$push, $each, $slice. See the documentation.
Here are a few ways to update the images array without overwriting it:
Update a Specific Index
Units.update({_id: id}, {$set: {'images.0': obj.images[0]}})
Append a Single Value
Units.update({_id: id}, {$push: {images: obj.images[0]}})
Append Multiple Values
Units.update({_id: id}, {$push: {images: {$each: obj.images}}})
I have a DB which has records with nested fields like :
"requestParams" : { "query" : [ "tv9" ] }
I am using the following query to fetch these kind of records. However I am looking for a more general case wherein the field query matches /tv9/ which includes tv9 as part of any search. Like it should also return livetv9, movietv9, etc. Following query does not seem to work:
db.requestLog.findOne({url : /search.json/, requestParams: {$elemMatch: {query: /tv9/}}})
$elemMatch is used to match partial document elements inside an array. Since you want to directly match on a string the use of this operator is inappropriate. Use this instead :
db.requestLog.findOne({url : /search.json/, 'requestParams.query': /tv9/})
Example :
mongos> db.test.save({r:{q:["tv9"]}})
mongos> db.test.save({r:{q:["tv"]}})
mongos> db.test.save({r:{q:["ltv9l"]}})
mongos> db.test.find({'r.q':/tv9/})
{ "_id" : ObjectId("4f5095d4ec991a74c16ba862"), "r" : { "q" : [ "tv9" ] } }
{ "_id" : ObjectId("4f5095deec991a74c16ba864"), "r" : { "q" : [ "ltv9l" ] } }
At this moment I have in Node.JS API written a function
Board.find({ users : req.user._id})
It will find all documents where is id of user inside of array users,
for example
So this function will find this document.
{
"_id" : ObjectId("5a7f4b46f489dd236410d88a"),
"name" : "first",
"users" : [
ObjectId("5a1db9e8db97d318ac70715d")
]
}
What If I will change array of users in document for array objects id
{
"_id" : ObjectId("5a7f4b46f489dd236410d77c"),
"name" : "second",
"users" : [
{ _id : ObjectId("5a1db9e8db97d318ac70715d") }
]
}
How to find now this document in this situation, using only req.user._id which is saved inside object of users[]?
we can find it somehow now or not??
You just need to change it as : Board.find({ 'users._id' : req.user._id})
You could also use:
db.collection_name.find( { "user": { "_id" : req.user._id } } )