Mongoose $nin with array of ObjectId - mongodb

I am facing a problem with mongoose. I can't do a query filter with $nin or $ne on an array of ObjectId.
Like:
query: {
objectIds: { $nin: [ objectId1, objectId2 ] }
}
I tried to send an ObjectId.toString(), an ObjectId, but nothing works.
However, if I replace the ObjectId array by a string array or put ObjectId in it, it works.
Any idea why it doesn't work with ObjectId?

Related

How to query nested arrays in mongoDB to find documents with elements that don't exist

I'm trying to make an aggregation query to find all documents that do not contain a certain element. It needs to be an aggregation because I want to be able to edit the returned documents. Eg. I only want to return some fields and I also want to be able to do a group on eg. the "producer" element.
I already tried practically all I can think of. I tried unwinding the arrays, but then I created even more documents where the element packagingInformation was mission than originally. I tried using $ne, $eq, $gt, $lte,.. to find the documents needed,... but they always return all documents because of the nested array structure.
$ArrayToObject didn't do the trick either for me.
I'm clueless on how to achieve this. The tripple nested array structure beats my imagination.
The only thing that returns me the wanted result is the following query:
db.product.find({
"json.productData.productInformation.details.packagingInformation": { $exists: false }
})
But this doesn't suffice since it's not an aggregate, thus it doens't allow me to continue to do queries with the results. And the $exists doesn't work in aggregates.
This is the JSON structure which I'm struggling with (dummy data).
{
_id: 5ckflsmdk543klmf543klmtrkmgdfm,
productNumber: 001,
json: {
productData: {
productNumber: 001,
producer: coca-cola,
productInformation: [
{
trackingInformation: {
lastUpdate: 01-01-12,
creationDate: 01-01-11
},
details: [
packagingInformation: [
quantity: 5,
size: 20cm
],
productType: drinks,
otherMeaningLessInformation: whatever,
andEvenMoreInformationInArrays: [
andTheInformationGoesOn: wow,
andOn: nastyArrayStructures
]
]
]
}
}
}
}
The wanted result would be to return all the documents that do not contain the packagingInformation array or the packagingInformation.quantity element.
or even better, to return all documents but with an extra field:
containsPackagingInformation: true/false. With false being the result of all documents that do not contain packagingInformation or packagingInformation.quantity.
$exists DOES WORK in a aggregation.
$exists works the same way it works in .find
you can form a query like:
db.collection.aggregate({ $match: {
$or: [
{
"json.productData.productInformation.details.packagingInformation.quantity": {
$exists: false
}
},
{
"json.productData.productInformation.details.packagingInformation": {
$exists: false
}
}
] } })
Try this query here with dummy data

how to find data in array of object in mongodb

I find in array of object and match _id and is_active both key
ex
{
_id:'12333333333333'
name:'test',
array:[{
id:'1233449',
is_active:true
},{
id:'7987979',
is_active:false
},{
id:'9558555',
is_active:true
},{
id:'2564654',
is_active:false
}]
}
find data using mongo query
db.getCollection('demo').find({'array.id':'7987979','array.is_active':false});
not working
The reason it isnt working for you is because you are running a find operation on the array directly.
When querying for things within an array you can use $elemMatch to obtain an entire document that contains the matching array element.
If you need a custom output you can use Aggregation, with the $unwind operation in the pipeline on the array field.
Try this out on your collection and understand what it is doing
db.collectionName.aggregate([{
$unwind:"$array"
},{
$match:{
$and:[
{"array.id":'your_id'},
{"array.is_active":boolean}
]
}
}
])

MongoDB update collection's data

I try to update an MongoDB data by using this code:
db.medicines.update({"_id":"586a048e34e5c12614a7424a"}, {$set: {amount:'3'}})
but unfortantly the query does not recognize the selector "_id":"586a048e34e5c12614a7424a", even if its exists.
Its succsed when I change the key to another like: name,rate and etc..
there is a special way to use update with _id parameter?
Thanks a head.
_id will be the unique ObjectId that mongodb generates for every document before inserting it. The query dint work because _id is an ObjectId and "586a048e34e5c12614a7424a" is a String. You need to wrap _id with ObjectId().
If you're using mongodb query
db.medicines.update({
"_id": ObjectId("586a048e34e5c12614a7424a")
}, {
$set: {
amount: '3'
}
});
If you are using mongoose. You can use findByIdAndUpdate
db.medicines.findByIdAndUpdate({
"_id": "586a048e34e5c12614a7424a"
}, {
$set: {
amount: '3'
}
});

how to get results from mongoDB where _id is string objectId

in my mongo database _id is string from ObjectId like this:
when I make this query I get results but with wrong sorting
db.collection.find({ _id : {$gt:"57c03e6288579757b5172d51"} });
how to fix that ?
You cannot query by inserting the objectID string with $gt operator.
You will have to create an objectID.
var objID = ObjectId("57c03e6288579757b5172d51");
db.collection.find({ _id : {$gt: objID} });
Documentation

MongoDB: multiple $in on the same field

I want to query mongoDB field for data that is '$in' two lists (arrays) kinda:
{user: {$in: [<array1>]} AND user: {$in: [<array2>]}}
so the query returns only those 'users' that present in both arrays.
I feel this isn't right:
{user: {$in: [<array1>], [<array2>]}}
Any ideas?
Either you can use the $AND operator.
http://docs.mongodb.org/manual/reference/operator/query/and/
{'$AND':[ {'user':{$in: [<..array1..>]} },{'user':{$in: [<..array2..>] } }] }