Querying mongodb with $in not working - mongodb

Why is this query not working?
>db.fs.files.find()
{ "_id" : ObjectId("550a34129c44a4fa03fb78ea"), "filename" : "mJnB2DhK.png", "contentType" : "binary/octet-stream", "length" : 28297, "chunkSize" : 261120, "uploadDate" : ISODate("2015-03-19T02:27:30.757Z"), "aliases" : null, "metadata" : { "id" : "mJnB2DhK" }, "md5" : "85944f92db940c6590fc8059db91ea16" }
{ "_id" : ObjectId("550a34ed48f6205504285a3b"), "filename" : "mkPQ6wnY.png", "contentType" : "binary/octet-stream", "length" : 28297, "chunkSize" : 261120, "uploadDate" : ISODate("2015-03-19T02:31:10.069Z"), "aliases" : null, "metadata" : { "id" : "mkPQ6wnY" }, "md5" : "85944f92db940c6590fc8059db91ea16" }
>db.fs.files.find({"metadata": {"id": { $in: ["mkPQ6wnY","mJnB2DhK"]}}})
>
I can't find the difference on this query against the one in the docs, except that my $in comparison is in a second level, but I don't think that would be a problem for MongoDB.
I guess is a very silly mistake, but I can't see it.

Try
db.fs.files.find({"metadata.id": { $in: ["mkPQ6wnY","mJnB2DhK"]}})

Related

Mongo shell search by field value returns no result, although field exists

A search without a query returns some documents. Then I try to query one of those documents by a field value, and the query returns nothing:
`
> db.packageData.find( { },{data : 0} ).limit(2).pretty()
{
"_id" : "RB#0122SS01CT1_20221024_164223_00116723.xml_1666644148902",
"filename" : "0122SS01CT1_20221024_164223_00116723.xml",
"p_uid" : "0122SS01CT1_20221024_164223_00116723",
"tunnelPackageId" : NumberLong(116723),
"userId" : "RB",
"contentType" : "CAMERA_XML",
"length" : 13652,
"uploadDate" : ISODate("2022-10-24T20:42:28.902Z"),
"forwardDate" : ISODate("2022-10-24T20:42:28.903Z"),
"forwarded" : false
}
{
"_id" : "TOP#0122SS01CT1_20221024_164225_00116726_3.jpg_1666644148991",
"filename" : "0122SS01CT1_20221024_164225_00116726_3.jpg",
"p_uid" : "0122SS01CT1_20221024_164225_00116726",
"tunnelPackageId" : NumberLong(116726),
"userId" : "TOP",
"contentType" : "THUMBNAIL_IMAGE",
"length" : 34556,
"uploadDate" : ISODate("2022-10-24T20:42:28.991Z"),
"forwardDate" : ISODate("2022-10-24T20:42:28.991Z"),
"forwarded" : false
}
> db.packageData.find( { "filename" : "0122SS01CT1_20221024_164223_00116723.xml" },{data : 0} ).pretty()
>
Thanks for any ideas!
`
I tried escaping a "." in the filename value, but got the same result.
you did something wrong, see output I got for your data:
MongoDB Enterprise replset:PRIMARY> db.coll.insertOne({
... "_id" : "RB#0122SS01CT1_20221024_164223_00116723.xml_1666644148902",
... "filename" : "0122SS01CT1_20221024_164223_00116723.xml",
... "p_uid" : "0122SS01CT1_20221024_164223_00116723",
... "tunnelPackageId" : NumberLong(116723),
... "userId" : "RB",
... "contentType" : "CAMERA_XML",
... "length" : 13652,
... "uploadDate" : ISODate("2022-10-24T20:42:28.902Z"),
... "forwardDate" : ISODate("2022-10-24T20:42:28.903Z"),
... "forwarded" : false
... })
{
"acknowledged" : true,
"insertedId" : "RB#0122SS01CT1_20221024_164223_00116723.xml_1666644148902"
}
MongoDB Enterprise replset:PRIMARY> db.coll.find( { },{data : 0} ).limit(2).pretty()
{
"_id" : "RB#0122SS01CT1_20221024_164223_00116723.xml_1666644148902",
"filename" : "0122SS01CT1_20221024_164223_00116723.xml",
"p_uid" : "0122SS01CT1_20221024_164223_00116723",
"tunnelPackageId" : NumberLong(116723),
"userId" : "RB",
"contentType" : "CAMERA_XML",
"length" : 13652,
"uploadDate" : ISODate("2022-10-24T20:42:28.902Z"),
"forwardDate" : ISODate("2022-10-24T20:42:28.903Z"),
"forwarded" : false
}
MongoDB Enterprise replset:PRIMARY> db.coll.find( { "filename" : "0122SS01CT1_20221024_164223_00116723.xml" },{data : 0} ).pretty()
{
"_id" : "RB#0122SS01CT1_20221024_164223_00116723.xml_1666644148902",
"filename" : "0122SS01CT1_20221024_164223_00116723.xml",
"p_uid" : "0122SS01CT1_20221024_164223_00116723",
"tunnelPackageId" : NumberLong(116723),
"userId" : "RB",
"contentType" : "CAMERA_XML",
"length" : 13652,
"uploadDate" : ISODate("2022-10-24T20:42:28.902Z"),
"forwardDate" : ISODate("2022-10-24T20:42:28.903Z"),
"forwarded" : false
}

Inconsistent query results with embedded documents on MongoDB

I've got a collection called payments with an example of its document shown below:
{
"_id" : ObjectId("579b5ee817e3aaac2f0aebc1"),
"updatedAt" : ISODate("2016-07-29T11:04:01.209-03:00"),
"createdAt" : ISODate("2016-07-29T10:49:28.113-03:00"),
"createdBy" : ObjectId("5763f56010cd7b03008147d4"),
"contract" : ObjectId("578cb907f1575f0300d84d09"),
"recurrence" : [
{
"when" : ISODate("2016-05-29T11:03:45.606-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e2d"),
"transaction" : {
"createdAt" : ISODate("2016-05-29T11:03:45.608-03:00"),
"tid" : "9999999999999999B01A",
"status" : 4,
"code" : "00",
"message" : "Transação autorizada"
},
"status" : "PAGO"
},
{
"when" : ISODate("2016-06-29T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e2c"),
"transaction" : {
"createdAt" : ISODate("2016-06-29T11:03:45.608-03:00"),
"tid" : "9999999999999999B01A",
"status" : 4,
"code" : "00",
"message" : "Transação autorizada"
},
"status" : "PAGO"
},
{
"when" : ISODate("2016-07-29T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e2b"),
"status" : "ERRO",
"transaction" : {
"code" : "56",
"createdAt" : ISODate("2016-07-29T11:04:01.196-03:00"),
"message" : "Autorização negada",
"status" : 5,
"tid" : "1006993069000730B88A"
}
},
{
"when" : ISODate("2016-07-30T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e2a"),
"status" : "PENDENTE"
},
{
"when" : ISODate("2016-07-31T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e29"),
"status" : "PENDENTE"
},
{
"when" : ISODate("2016-08-01T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e28"),
"status" : "PENDENTE"
}
],
"status" : "PAGO",
"conditions" : {
"originalValue" : 7406.64,
"totalValue" : 7400,
"upfrontValue" : 1500,
"upfrontInstallments" : 3,
"balanceInstallments" : 9
},
"__v" : 0,
"transaction" : {
"code" : "00",
"createdAt" : ISODate("2016-07-29T10:49:46.610-03:00"),
"message" : "Transação autorizada",
"status" : 6,
"tid" : "1006993069000730AF5A"
}
}
If I run the query below, I get the desired document shown above:
db.payments.find({ "recurrence.transaction.tid": "1006993069000730B88A" })
However, if I run this other query, MongoDB returns my entire collection (presumably because it didn't match the subdocument's id):
db.payments.find({ "recurrence._id": ObjectId("579b6241ea945e3631f64e2b") })
Both queries should return the same result! I also checked some other questions including this one so unless I'm going crazy I'm doing the same thing. Not sure why the inconsistent results though.
Tryout this:
db.payments.find({ recurrence : { $elemMatch: { "transaction.tid": "1006993069000730B88A"} } }).pretty()

mongo query doesn't work with $ in field name

Command to get raw_data:
db.raw_data.find({'cat':'like'},
{'properties':1}).limit(1).pretty()
data:
{
"_id" : ObjectId("5656b9a0c2492dec3442da52"),
"properties" : {
"subcategory" : "49",
"$carrier" : "Vodafone India",
"$radio" : "HSDPA",
"$region" : "Gujarat",
"$screen_width" : 375,
"$wifi" : false,
"mp_lib" : "iphone",
"product_unlike_flag" : false,
"mp_device_model" : "iPhone7,2",
"user_id" : "4",
"$city" : "Ahmedabad",
"$manufacturer" : "Apple",
"$os" : "iPhone OS",
"brand" : "AO",
"gender" : "Men",
"mp_country_code" : "IN",
"time" : 1445376786,
"$app_release" : "0.8.0",
"$lib_version" : "2.8.2",
"$model" : "iPhone7,2",
"$screen_height" : 667,
"category" : "48",
"$app_version" : "0.8.0",
"$os_version" : "9.0.2",
"itemcode" : "174",
"source" : "Product"
}
}
I want to extract user_id and city from this data.
I tried these Command :
Command1 :
db.raw_data.aggregate([{$group : {_id :{'user_id': "$properties.user_id","cat":"$cat","brand":"$properties.brand" } ,"num_tutorial" :{$sum:1}}} ,{ $project : {properties.$city : 1 } } ])
Command2:
db.raw_data.find({'cat':'like'},{'properties.$city':1})
which gave me an error:
Error: error: {
"$err" : "Can't canonicalize query: BadValue Positional projection 'properties.$city' does not match the query document.",
"code" : 17287
}
Is there any way/query so I can extract user-city relations from this data.
In suck cases mongodb recommends to use unicode equivalent of '$'. This method is called $ sign escaping.
So your find query will look like following:
db.raw_data.find({'cat':'like'},{'properties.\uff04city':1})
Hope this helps :)
Result:
> db.collection.find({},{'properties.\uff04city':1})
{ "_id" : ObjectId("5656e09ccb0a925b3d5d16f2"), "properties" : { "$city" : "value" } }
{ "_id" : ObjectId("5656e502cb0a925b3d5d16f3"), "properties" : { "$city" : "value" } }

How do i remove array item from collection

How do i remove array item from collection in mongodb? Below is my structure.
{
"__v" : 12,
"_id" : ObjectId("52cc27daasdsc797ec7s8000001"),
"email" : "email#email.com",
"joindate" : ISODate("2014-01-07T16:14:18.158Z"),
"location" : "Canada",
"name" : "Albert",
"oauthID" : 58558855,
"myvideos" : [
{
"vid" : "wssEoGquzsiw1c",
"date" : ISODate("2014-06-01T05:28:32.713Z")
},
{
"vid" : "0_Vko7RXsdsmoI",
"date" : ISODate("2014-06-01T05:29:01.888Z")
},
{
"vid" : "ctbvhucsds050g",
"date" : ISODate("2014-06-01T05:29:27.395Z")
}
],
"username" : "myusername"
}
I would like to remove item vid (ctbvhucsds050g) from the myvideos array.
Appreciate any advice.
Found a solution to this.
db.users.update({"name":"Albert"}, {'$pull': {"myvideos": {"vid": "ctbvhucsds050g" }}});

MongoDB query to break ties and remove duplicates

I have documents which have a Version, URL, and DateAdded field (among others but these are the relevant ones).
I'd like to find all documents where the Version is "5.5" and the DateAdded is less than or equal to January 1, 2013. That's pretty straightforward, but I also want the following behavior:
If two or more documents have the same URL, only return the one with the most recent DateAdded (provided, again, that is is less than or equal to January 1, 2013). It would be great if all of this could be expressed in a single query (but my main concern is performance).
I've been doing this last bit of filtering in my client code (outside of MongoDB) but this ends up being inefficient, not to mention inelegant.
I've also tried using Mongo's MapReduce functionality to accomplish the same thing but this is extremely slow, as it appears to copy much of my collection to another collection.
Is there a performant solution?
This should do the trick.
Example data:
db.foo.insert({ "_id" : ObjectId("528bd5bded29286a62959513"), "Version" : "5.3", "URL" : "foo.bar.com/asdfwoaef", "DateAdded" : ISODate("2012-10-05T00:00:00Z") })
db.foo.insert({ "_id" : ObjectId("528bd5e8ed29286a62959514"), "Version" : "5.6", "URL" : "foo.bar.com/asdfwoaef", "DateAdded" : ISODate("2012-12-05T00:00:00Z") })
db.foo.insert({ "_id" : ObjectId("528bd621ed29286a62959515"), "Version" : "5.5", "URL" : "foo.bar.com/aafoobbb", "DateAdded" : ISODate("2012-11-04T00:00:00Z") })
db.foo.insert({ "_id" : ObjectId("528bd629ed29286a62959516"), "Version" : "5.5", "URL" : "foo.bar.com/aafoobbb", "DateAdded" : ISODate("2012-11-05T00:00:00Z") })
db.foo.insert({ "_id" : ObjectId("528bd642ed29286a62959517"), "Version" : "5.5", "URL" : "foo.bar.com/aafoobbb", "DateAdded" : ISODate("2013-01-02T00:00:00Z") })
db.foo.insert({ "_id" : ObjectId("528bd744ed29286a62959518"), "Version" : "5.5", "URL" : "foo.bar.com/ccbarcc", "DateAdded" : ISODate("2013-01-02T00:00:00Z") })
db.foo.insert({ "_id" : ObjectId("528bd780ed29286a62959519"), "Version" : "5.5", "URL" : "foo.bar.com/ccbarcc", "DateAdded" : ISODate("2012-04-05T00:00:00Z") })
Pipeline:
pipeline = [
{
"$match" : {
"Version" : "5.5",
"DateAdded" : {
"$lt" : ISODate("2013-01-01T00:00:00Z")
}
}
},
{
"$sort" : {
"URL" : 1,
"DateAdded" : -1
}
},
{
"$group" : {
"_id" : "$URL",
"doc" : {
"$first" : {
"id" : "$_id",
"DateAdded" : "$DateAdded"
}
}
}
}
]
db.foo.aggregate(pipeline)
And here is the result:
{
"result" : [
{
"_id" : "foo.bar.com/ccbarcc",
"doc" : {
"id" : ObjectId("528bd780ed29286a62959519"),
"DateAdded" : ISODate("2012-04-05T00:00:00Z")
}
},
{
"_id" : "foo.bar.com/aafoobbb",
"doc" : {
"id" : ObjectId("528bd629ed29286a62959516"),
"DateAdded" : ISODate("2012-11-05T00:00:00Z")
}
}
],
"ok" : 1
}