not able to fetch using $and in mongo db - mongodb

I'm trying to use $and in mongodb stitch function to get data using 2 different condition
data = col.find( { $and: [ { "title": { title } }, { "address.countryName": { country } } ] } ).toArray;
but this shows $undefined:true in response.
please guide what is wrong in here

You need to remove {} in the condition's value
db.getCollection('test').find({
$and: [
{
"title": 'title' --> Here
},
{
"address.countryName": 'country' --> Here
}
]
})

Related

MongoDB: Can't update in nested arrays

I've been trying to modify a value in multiple arrays for a few arrays and I can't find documentation on how to do this.
My collection looks like this
"rates": [
{
"category": "Web",
"seniorityRates": [
{
"seniority": "junior",
"rate": 100
},
{
"seniority": "intermediate",
"rate": 135
},
{
"seniority": "senior",
"rate": 165
}
]
}
]
I'm just trying to modify "junior" to "beginner", this should be simple.
Thanks to these answers:
How can I update a multi level nested array in MongoDB?
MongoDB updating fields in nested array
I've manage to write that python code (pymongo), but it doesn't works...
result = my_coll.update_many({},
{
"$set":
{
"rates.$[].seniorityRates.$[j].seniority" : new
}
},
upsert=False,
array_filters= [
{
"j.seniority": old
}
]
)
The path 'rates' must exist in the document in order to apply array updates.
It correspond to this command that doesn't work either
db.projects.updateMany({},
{
$set:
{
"rates.$[].seniorityRates.$[j].seniority" : "debutant"
}
},
{ arrayFilters = [
{
"j.seniority": "junior"
}
]
}
)
clone(t={}){const r=t.loc||{};return e({loc:new Position("line"in r?r.line:this.loc.line,"column"in r?r.column:......)} could not be cloned
What am I doing wrong ?
Any help would be very appreciated
The other option could be Sample
db.collection.update({},
{
$set: {
"rates.$[].seniorityRates.$[j].seniority": "debutant"
}
},
{
arrayFilters: [
{
"j.rate": { //As per your data, you can apply the condition o rate field to modify the level
$lte: 100
}
}
]
})
Or
The actual query should work Sample
db.collection.update({},
{
$set: {
"rates.$[].seniorityRates.$[j].seniority": "debutant"
}
},
{
arrayFilters: [
{
"j.seniority": "junior"
}
]
})
The same should work in python, a sample question
So I was just dumb here, I inverted two parameters so I didn't have the correct collection in the python code...
Thanks Gibbs for pointing out where the mistake was in the mongo command.
I will not delete this post as it can help other to know how to do this kind of queries.

How to write a query in mongo db to find elements in which the nested array does not have an element with the given id?

There is a given array with data .
[
{
"_id":"62d9553f2f7d45d51e9e6850",
"title":"lol",
"body":"lol",
"readIds":[
{
"id":"62c56b2cffe059fdfdd19230",
"createdAt":1658417024700
}
],
"mode":"custom",
"createdAt":"2022-07-21T13:31:43.633Z",
"updatedAt":"2022-07-21T15:23:44.701Z"
},
{
"_id":"62d954e2ea35817546d42702",
"title":"tesssst",
"body":"tesssst",
"readIds":[
],
"mode":"custom",
"createdAt":"2022-07-21T13:30:10.759Z",
"updatedAt":"2022-07-21T13:30:10.759Z"
},
]
And I need to write a query to filter the array and get only those elements which readIds hasn't object with id equal to '62c56b2cffe059fdfdd19230'.
My attempts were like this:
.find({
readIds: {
$elemMatch: {
id: {
$not: {
$eq: id,
},
},
},
},
})
I'm not sure i got your question correctly, but isn't the following working?
db.getCollection('stuff').find({"readIds.id": {$ne: "62c56b2cffe059fdfdd19230"}})
you can test it here: Mongo Playground

Filter arrays in mongodb

I have a collection where each document contains 2 arrays of documents as below.
{
all_users:[
{
id:1,
name:"A"
},
{
id:2,
name:"B"
},
{
id:3,
name:"C"
}
]
selected_users:[
{
id:1,
name:"A"
},
{
id:2,
name:"B"
}
]
}
Is it possible to merge the 2 arrays by adding a field selected and assigning a value to it based on whether the name of the user is present in a document in the selected_users array as follows.
{
all_users:[
{
id:1,
name:"A",
selected:"yes"
},
{
id:2,
name:"B",
selected:"yes"
},
{
id:3,
name:"C",
selected:"no"
}
]
}
I want to check by id or name since the documents may contain additional fields. I can't figure out how to achieve this.
$map to iterate loop of all_users array
$cond check condition if id is in selected users id then return "yes" otherwise "no" in selected field
$mergeObject to merge current user object with above selected field
db.collection.aggregate([
{
$project: {
all_users: {
$map: {
input: "$all_users",
in: {
$mergeObjects: [
"$$this",
{
selected: {
$cond: [
{ $in: ["$$this.id", "$selected_users.id"] },
"yes",
"no"
]
}
}
]
}
}
}
}
}
])
Playground
If you are using the Robo Mongo 3T, you can use JavaScript, so you can do something like that:
db.collection.find({}).forEach(doc => {
doc.all_users.map(au => {
const su = doc.selected_users.find(su => su.id === au.id);
au.selected = su === undefined;
});
delete doc.selected_users;
db.collection.save(doc);
})
I would advise you to create a Backup of the collection beforehand. :)

how to filter the fields of a document within another document in mongodb?

My document is the following
{
"name":"Name1",
"status":"active",
"points":[
{
"lag":"final"
},
{
"lag":"final"
}
]
},
{
"name":"Name2",
"status":"active",
"points":[
{
"lag":"final"
},
{
"lag":""
}
]
}
I need to get all the documents that have some value in the lag field, for this example should get two document,
I tried with this query, but it only returns me when all points have full lag
{ "points.lag":{$not:{ $eq:"" }},status:{$in:['active']}}
Play
You need to use elemMatch to check whether atleast one element matches the condition.
db.collection.find({
"points": {
"$elemMatch": {
"lag": {
$ne: null
}
}
}
})

Fetch records if field exists in mongodb

I have this query
db.invites.find({$and: [{primaryowner: mynumber}, {invited: mynumber}]})
which i am using to fetch records if they satisfy the conditions above.However, i would like to fetch records where {eventtype:4} and the field primaryowner:$exists: true
This is what i have
{ eventtype: 4 },{ primaryowner: { $exists: true }}
but i know this is not it because i need to fetch records where eventtype = 4 and the field primaryowner exists.
How can i represent the query?.
From your question, I couldnot clearly understand what is the required business scenario, but with the sample data provided
I have given an pseudo code and the respective mongodb implementation of that pseudo code.
if invited field is "0800123456"
if event type is 3 and event status is 11
select matching documents
or if event type is 4 and event status is 11 and primary owner exists
select matching documents
Mongo DB Query
db.collection.find({
$or: [
{
$and: [
{
"invited": "0800123456"
},
{
"eventtype": {
$eq: "3"
}
},
{
"eventstatus": {
$eq: 11
}
}
]
},
{
$and: [
{
"eventtype": {
$eq: "4"
}
},
{
"eventstatus": {
$eq: 11
}
},
{
primaryowner: {
$exists: true
}
}
]
}
]
})
If this is not matching your scenario, then write your own query with the help of this sample code.
Hope it Helps!