Combination of and or in mongo db query - mongodb

I am trying to debug and issue with my mongo db queries, the one that I m trying to use now looks like this:
mongo query {
'$and': {
'$or': { name: /aaaq/i, 'metadata.description': /aaaq/i },
'$and': {
'admin.parentPath': '3e98c9f0-cbe9-4c27-b1af-ba43069907cc',
'admin.sharedWith': '78b43de0-47c2-495b-a3ad-afc9fb6c9815'
}
}
}
The ide is that I want to check if the name or metadata respect the same regex expression and if the admin.parentPath and admin.sharedWith have the correct value, is this query written correctly?
The initial query looked like this:
mongo query {
name: /aaaq/i,
'metadata.description': /aaaq/i,
'admin.parentPath': '3e98c9f0-cbe9-4c27-b1af-ba43069907cc',
'admin.sharedWith': '78b43de0-47c2-495b-a3ad-afc9fb6c9815'
}
But I think that this one put AND between all conditions

Related

Mongo db query is returning documents with different values

I'm using morphia ORM to search on a list of values separated by commas, The problem is the query is returning values who not match with the option passed on the query.
I'm really lost about this. The models is something like this.
{
user: "",
answeredQuestions: [
{
type: "vehicules",
answerd: "car,motorbike,bicycle"
}
{
type: "vehicules",
answerd: "motorbike"
}
],
}
And if pass the query {'answeredQuestions.answerd': { $regex: 'car' }} is returning both documents.
And this is and example of the return real response of the BD on this image:
error example
Thanks for the help!

Object Query in MongoDB - Compass

I'd like to execute an object query about how many documents have "JAVA" in the variable L5_tecnologias:
Screenshot with the data structure of variable L5_tecnologias
As you can see in the screenshot, I use the formula shown in the MongoDB manual (https://docs.mongodb.com/manual/tutorial/query-embedded-documents/), but it does not work. So I've tried the commands:
{ L5_tecnologias: { JAVA: "" } }
{ L5_tecnologias: ["JAVA"] }y
{ L5_tecnologias: "JAVA" }
but none of them work. Anyone can help?
Use dot notation with $exists.
db.collection.find({
"L5_tecnologias.JAVA": {
$exists: true
}
})
Sample Mongo Playground

MongoDB aggregation $unwind equivalent of ElasticSearch

I'm pretty new to ElasticSearch, I recently indexed one of my MongoDB collection on ElasticSearch using compose transporter. The reason I indexed is I wanted to do full-text search on some of the fields.
So, now I have a nested document like below,
{
"item":"journal",
"item_size":200,
"instock":[
{
"warehouse":"A",
"qty":5
},
{
"warehouse":"A",
"qty":15
}
]
}
If I want to do a MongoDB aggregate on the doc to match where warehouse is A and $unwind them,
[{"$unwind":"$instock"},{"$match":{"instock.warehouse":"A"}}]
I'll get the following result,
{
"item":"journal",
"item_size":200,
"instock":
{
"warehouse":"A",
"qty":5
}
}
{
"item":"journal",
"item_size":200,
"instock":
{
"warehouse":"A",
"qty":15
}
}
This is just a simple example but I have more nested complex documents like this in MongoDB. How can I achieve the same result in the ElasticSearch?
I spent a lot of time figuring out this but no luck.

Parse Server aggregate using match on date column

I'm using parse-server version 2.7.4 with parse (the JavaScript interface) version 1.11.1. I'm trying to execute an aggregate query using match on a date column. Here's what my pipeline looks like:
[
{
match: {
'_created_at': new Date('2018-04-01T00:00:00.000Z')
}
}
]
I most definitely have records in the database greater than that date, yet this query returns me zero records in JavaScript. However, if I run it in the Mongo shell, I get documents returned. Here is what I execute in the Mongo shell:
[
{
$match: {
'_created_at': ISODate('2018-04-01T00:00:00.000Z')
}
}
]
Am I doing something wrong with the parse JavaScript API or does it not support match aggregation on date columns? Thanks for any help.

Update with expression instead of value

I am totally new to MongoDB... I am missing a "newbie" tag, so the experts would not have to see this question.
I am trying to update all documents in a collection using an expression. The query I was expecting to solve this was:
db.QUESTIONS.update({}, { $set: { i_pp : i_up * 100 - i_down * 20 } }, false, true);
That, however, results in the following error message:
ReferenceError: i_up is not defined (shell):1
At the same time, the database did not have any problem with eating this one:
db.QUESTIONS.update({}, { $set: { i_pp : 0 } }, false, true);
Do I have to do this one document at a time or something? That just seems excessively complicated.
Update
Thank you Sergio Tulentsev for telling me that it does not work. Now, I am really struggling with how to do this. I offer 500 Profit Points to the helpful soul, who can write this in a way that MongoDB understands. If you register on our forum I can add the Profit Points to your account there.
I just came across this while searching for the MongoDB equivalent of SQL like this:
update t
set c1 = c2
where ...
Sergio is correct that you can't reference another property as a value in a straight update. However, db.c.find(...) returns a cursor and that cursor has a forEach method:
Queries to MongoDB return a cursor, which can be iterated to retrieve
results. The exact way to query will vary with language driver.
Details below focus on queries from the MongoDB shell (i.e. the
mongo process).
The shell find() method returns a cursor object which we can then iterate to retrieve specific documents from the result. We use
hasNext() and next() methods for this purpose.
for( var c = db.parts.find(); c.hasNext(); ) {
print( c.next());
}
Additionally in the shell, forEach() may be used with a cursor:
db.users.find().forEach( function(u) { print("user: " + u.name); } );
So you can say things like this:
db.QUESTIONS.find({}, {_id: true, i_up: true, i_down: true}).forEach(function(q) {
db.QUESTIONS.update(
{ _id: q._id },
{ $set: { i_pp: q.i_up * 100 - q.i_down * 20 } }
);
});
to update them one at a time without leaving MongoDB.
If you're using a driver to connect to MongoDB then there should be some way to send a string of JavaScript into MongoDB; for example, with the Ruby driver you'd use eval:
connection.eval(%q{
db.QUESTIONS.find({}, {_id: true, i_up: true, i_down: true}).forEach(function(q) {
db.QUESTIONS.update(
{ _id: q._id },
{ $set: { i_pp: q.i_up * 100 - q.i_down * 20 } }
);
});
})
Other languages should be similar.
//the only differnce is to make it look like and aggregation pipeline
db.table.updateMany({}, [{
$set: {
col3:{"$sum":["$col1","$col2"]}
},
}]
)
You can't use expressions in updates. Or, rather, you can't use expressions that depend on fields of the document. Simple self-containing math expressions are fine (e.g. 2 * 2).
If you want to set a new field for all documents that is a function of other fields, you have to loop over them and update manually. Multi-update won't help here.
Rha7 gave a good idea, but the code above is not work without defining a temporary variable.
This sample code produces an approximate calculation of the age (leap years behinds the scene) based on 'birthday' field and inserts the value into suitable field for all documents not containing such:
db.employers.find({age: {$exists: false}}).forEach(function(doc){
var new_age = parseInt((ISODate() - doc.birthday)/(3600*1000*24*365));
db.employers.update({_id: doc._id}, {$set: {age: new_age}});
});
Example to remove "00" from the beginning of a caller id:
db.call_detail_records_201312.find(
{ destination: /^001/ },
{ "destination": true }
).forEach(function(row){
db.call_detail_records_201312.update(
{ _id: row["_id"] },
{ $set: {
destination: row["destination"].replace(/^001/, '1')
}
}
)
});