Mongodo db delete documents between custom fields - mongodb

I have below documents in mongodb, am trying to delete the documents based on the referenceId field between the values X0000000005 and X00000000010, I couldnt find any articles for deleting mongo documents based on custom field, can someone please help me to do this deletion if its possible?
{
"_id" : ObjectId("5a0f13ad0a83924b84d16b7d"),
"senderId" : "783",
"clientId" : "146196",
"referenceId" : "X00000000001",
"file" : "jAAAAAECAAABaAAAAKQAAJyMKYqPYvFQKJrZ/fqYjDKNdXdOMK58tPQ"
}
{
"_id" : ObjectId("5a0f13ad0a83924b84d16b7e"),
"senderId" : "783",
"clientId" : "146196",
"referenceId" : "X00000000002",
"file" : "jAAAAAECAAABaAAAAKQAAJyMKYqPYvFQKJrZ/fqYjDKNdXdOMK58tPQ"
}
.
.
.
.
.
.
{
"_id" : ObjectId("5a0f13ad0a83924b84d16b7f"),
"senderId" : "783",
"clientId" : "146196",
"referenceId" : "X00000000020",
"file" : "jAAAAAECAAABaAAAAKQAAJyMKYqPYvFQKJrZ/fqYjDKNdXdOMK58tPQ"
}

The following simple query should work:
db.collection.remove({"referenceId":{$gte:"X00000000005"}, "referenceId":{$lte:"X00000000010"}})
You might want to run a find() using the same filter first in order to make sure that the delete() will affect the right records. That'd obviously be this then:
db.collection.find({"referenceId":{$gte:"X00000000005"}, "referenceId":{$lte:"X00000000010"}})
Also, depending on the exact definition of your
between the values X0000000005 and X00000000010
you might need to swap the $lte and $gte operators out for something else ($gt and/or $lt).

db.collection.remove({"referenceId": {"$lte": "X00000000010", "gte": "X0000000005"}})

Related

(MongoDB) Aggregate (.out) moove values to wrong fields

I'm actually creating an autocomplete website bar using express js & mongodb 4.2.7, and using lat & lon to avoid using geocoding api.
Here is the format of my db:
{
"_id" : ObjectId("5eea03e9a7891b6d701df571"),
"id_ban_position" : "ban-position-4c882de48d894fc49ed9be76f7631d6d",
"id_ban_adresse" : "ban-housenumber-78a2651ce382476ca9c8c8fcbcbaa539",
"cle_interop" : "01001_A028_5307",
"id_ban_group" : "ban-group-37c7c3f2b61440b48bca7d8fad56055e",
"id_fantoir" : "01001A028",
"numero" : 5307,
"suffixe" : "",
"nom_voie" : "Lotissement les Lilas",
"code_postal" : 1400,
"nom_commune" : "L'Abergement-Clémenciat",
"code_insee" : 1001,
"nom_complementaire" : "",
"x" : 848436.205131189,
"y" : 6562595.33916435,
"lon" : 4.923279,
"lat" : 46.146903,
"typ_loc" : "parcel",
"source" : "dgfip",
"date_der_maj" : "2019-02-12"
}
as you can see, i got lot of fields that are not necessary for the purpose of my website, and most of all, i don't need to get one row for each number in a street, one unique name of street is enough. So i decided to supress duplicate street name ('nom_voie') and town name ('nom_commune') with aggregate as follow:
db.Addresses.aggregate(
[ {
$group: {
_id: {voie: "$nom_voie", commune: "$nom_commune"},
doc: {$first: "$$ROOT"}
}
},
{$replaceRoot: {newRoot: "$doc"}},
{$out: 'UniqueIds'}
],
{allowDiskUse: true }
);
The problem is that this utilisation mooved a lot of values from one field to an other and made my db absolutely unusable.
{
"_id" : ObjectId("5eea03e9a7891b6d701df571"),
"nom_voie" : "-",
"code_postal" : 1400,
"nom_commune" : "Lotissement les Lilas",
"nom_complementaire" : "",
"lon" : 6562595.33916435,
"lat" : 848436.205131189,
"field19" : "dgfip",
...
}
As you can see, the value of "nom_voie" is now in "nom_commune", "x" value is in "lat", and "y" value is in "lon" and finally the "nom_voie" value is now "-", and i got new fields replacing others ("field19" for "source")...
Am i using aggregate with a wrong option?
I got 47 Millions entry, is this creating some issues?
Thanks to all of you for your time, even if you just reading this!
EDIT :
After few tries and some search i found out that it is the aggregate function that create some problems, but i still don't understand why, if somebody got some hints, i just edited the doc so it's more understandable and readable!
(I thought before that it was a $unset problem in a function)

Mongo search by short id that is not an ObjectId

We have a collection of documents representing document upload jobs, like the following:
{
"_id" : NumberLong(807106),
"_class" : "com.*.FileUploadJob",
"createdDate" : ISODate("2018-12-12T17:04:32.042Z"),
"jobConfigurationId" : NumberLong(5382),
"fileName" : "807106.xlsx",
"rowCount" : NumberLong(2),
"successfullyStagedRowsCount" : NumberLong(1),
"status" : "COMMIT_COMPLETED",
"rowProcessingComplete" : true
}
When I try to find this record, none of the following works:
db.fileUploadJob.find({"_id": ObjectId(807106)})
db.fileUploadJob.find({"_id": ObjectId("807106")})
db.fileUploadJob.find(ObjectId("807106"))
db.fileUploadJob.find({"_id": "807106"})
db.fileUploadJob.find({"_id": 807106})
...
How do I find that elusive entry?
Found the answer!
db.fileUploadJob.find({"_id": NumberLong(807106)})
I had to copy and paste the document JSON to a text editor from Robo3T to see what type it was!

mongo db update subdocument - subdocument not in array

I have a document like this,
{
"S" : {
"500209" : {
"total_income" : 38982,
"interest_income" : 1714,
"reported_eps" : 158.76,
"year" : 201303
}
},
"_id" : "pl"
}
I am trying to update this document like this,
{
"S" : {
"500209" : {
"total_income" : 38982,
"interest_income" : 1714,
"reported_eps" : 158.76,
"year" : 201303,
"yield": 1001, <== inserted a new attribute
}
},
"_id" : "pl"
}
I have tried this,
db.my_collection.update({_id: 'pl'},{$set: {'S.500209.yield': 1}})
But I couldn't make it. And I searched in stack overflow and google but I couldn't find out.
I got so many answers but most of them keeping sub-docuemnts in array.
Pleas help me to solve my issue, and please tell me why most of them keeping subdocuments in array.
Number key might cause the problem. Update your field name.
db.my_collection.update({_id: 'pl'},{$set: {'S.a500209.yield': 1}})
EDIT
Upgrade mongo version. It works fine with 2.4.

Mongo Command LIne Querying Embedded Document by Object Id

I have following in my accounts collection:
{ "_id" : ObjectId("4fc55125476e0a27d9000003"),
"created_at" : ISODate("2012-05-29T22:43:49Z"),
"teachers" : [ {
"_id" : ObjectId("4fc55125476e0a27d9000004"),
"updated_at" : ISODate("2012-05-29T22:43:49Z"),
"created_at" : ISODate("2012-05-29T22:43:49Z")
} ],
"updated_at" : ISODate("2012-05-29T22:43:49Z")
}
I want to query for an account that has a teacher with _id 4fc55125476e0a27d9000003.
If I use the command
db.accounts.findOne({"teachers._id" : ObjectId("4fc55125476e0a27d9000004")})
it returns null.
Also, why does the mongo command line thing hang when I use find instead of findOne with the above command.
As others pointed out, the query actually works. Not sure what was going on the other day that I couldn't get it to return the correct result. Maybe I was using an incorrect db. Thanks for the help and apologies for wasting your time.

MongoDB : query result size greater than collection size

I'm analyzing a MongoDB data source to check its quality.
I'm wondering if every document contains the attribute time: so I used this two command
> db.droppay.find().count();
291822
> db.droppay.find({time: {$exists : true}}).count()
293525
How can I have more elements with a given field than the elements contained in whole collection ? What's going wrong ? I'm unable to find the mistake.
If it's necessary I can post you the expected structure of the document.
Mongo Shell version is 1.8.3. Mongo Db version is 1.8.3.
Thanks in advance
This is the expected structure of the document entry:
{
"_id" : ObjectId("4e6729cc96babe974c710611"),
"action" : "send",
"event" : "sent",
"job_id" : "50a1b7ac-7482-4ad6-ba7d-853249d6a123",
"result_code" : "0",
"sender" : "",
"service" : "webcontents",
"service_name" : "webcontents",
"tariff" : "0",
"time" : "2011-09-07 10:22:35",
"timestamp" : "1315383755",
"trace_id" : "372",
"ts" : "2011-09-07 09:28:42"
}
My guess is that is an issue with the index. I bet that droppay has an index on :time, and some unsafe operation updated the underlying collection without updating the index.
Can you try repairing the db, and see if that makes it better.
Good luck.
There are probably time values that are of type array.
You may do db.droppay.find({time: {$type : 4}}) to find such documents.