If the data in my mongodb is like this
{
"category": "Pop Culture",
"subcategory": "Film Directors",
"name": "Quentin Tarantino",
}
Is there any way I can find this object only using some part of the name
Like:
db.data.find_one({'name': 'quentin'})
For string data, you can use $regex operator to find with some part of the field data.
Example:
db.collection.findOne({
name: {
$regex: "Some part of name"
}
})
See $regex for more information.
Related
I’ve been trying to update the data in my mongoDB.
I want to update all products with a new productName field.
my data looks something like:
{
"id": "12345",
"products": [{
"id": 0
"productCode": "test",
"status": "PENDING",
},
{
"id": 1
"productCode": "test",
"status": "COMPLETE",
}],
}
When I try the following. I get this error The positional operator did not find the match needed from the query.
db.customers.updateMany(
{ id: "12345" },
{ $set: {
"products.$.productName": "Name here" }
}
)
If I do account.0.productName then it’s fine and updates. I’m not sure why $ is not working for me
db.customers.updateMany(
{ id: "12345" },
{ $set: {
"products.0.productName": "Name here" }
}
)
Positional operator is not working because you are not using the array into the find (first object)
If you try this query it will work as expected because you have the position finding by products.id.
Otherwise, if you don't have the position into array where update, yo can't use $ operator in this way. You need this query:
db.collection.update({
"id": "12345",
},
{
"$set": {
"products.$[].newField": "test2"
}
},
{
"multi": true
})
Mongo playground example here
Using $[] you can reference the array and add the value into each object.
$[] docs here
It says:
The all positional operator $[] indicates that the update operator should modify all elements in the specified array field.
That's exactly we want :)
I have got a collection of documents and each documents contains a nested array of objects.
{
"id": "309324739",
"debters": [
{
"user": {
"name": "John Doe",
"internal": true
},
"debt": 1463,
},
{
"user": {
"name": "Alex Tree",
"internal": false
},
"debt": 53443,
},
}
What I'm trying to do is to return find the document by id and then find inside the debters list that has a false flag?
I tried the following query...
Debters findByIdAndDebters_User_InternalIsFalse(#Param("id") String id,);
But I'm getting an error saying that it can find "internal" property. What am I doing wrong and how can I loop through array using this magic mongo repository query?
you need to write a native query for that which is similar to
#Query("{'debters.user.internal':false,'_id':''}")
Debters findByIdAndDebtersUserInternalIsFalse(#Param("id") String id,);
I have mongodb document with data as following :
[{
"name": "Robin singh",
"developer": "java",
"address": "Robin Singh,Mohali",
"search_string": ["Robin", "Singh", "java"]
}, {
"name": "Rohan singh",
"developer": "java",
"address": "Rohan Singh,Mohali",
"search_string": ["Rohan", "Singh", "java"]
}]
I want to search document with developer java with name Rohan singh and I used this query:
{"search_string":{"$all":["Robin","Singh","java"]}}
but I am getting both results.
If you have both name and developer then you can simply use find query -
db.collection.find({"name":"Rohan singh", "developer":"java"})
Additionally, If you want to check when search_string contains exact parameters which you are passing in $all then - You need to use combination of $size and $all operator to get desired result. size must be the number of parameters that you are using in $all. Here you must check size of search_string to number of params in $all so that it will search given parameters ($all) only in those search_string which size matches to number of params.
Following query might be helpful to you.
db.collection.find({
"name":"Rohan singh",
"search_string": {
$all: ["Rohan", "singh","java"]
},
"search_string": {
$size: 3 // This is the number of param you pass in $all
}
}).pretty()
Try:
db.testcollection.aggregate([
{ $match: {"search_string":{"$all":["Robin","Singh","java"]}} },
{ $group: { _id: "$name"} }
])
Assuming this data structure:
{
"title": "Foo bar",
"username": "jackwreid"
},
{
"title": "Bish bosh",
"username": "lizziebump"
},
{
"title": "Ham nam",
"username": "lizziebump"
},
{
"title": "Blub blub",
"username": "jvarn"
}
And assuming
var userFollowing = ["lizziebump", "jackwreid"]
How would I do a db.posts.find() for posts where the username matches any of the contents of the userFollowing array?
I'm trying to build a query that returns posts by users in the current user's following list and I can only find docs on how to do this the other way around where the query returns posts if a single query string is in any array position.
You can use the $in operator
db.collection.find({username: {$in: userFollowing}})
Is it possible to use something like 'exists' in a Mongo query to return this record based on an ID?
Something like select where 'ids' contains key '123456'?
{
"department": "Digging",
"ids": {
"123456": {
"color": "blue"
},
"123457": {
"color": "red"
}
}
}
As you're searching for the existence of a field with a given name, $exists is the operator you need (see Advanced Queries).
e.g. something like:
db.YourCollection.find({ "ids.123456" : {$exists: true}});