cosmosdb mongo api not working for some commands - mongodb

I am using cosmosdb on azure
In that I am using mongodb api
I have a "request" collection inside that there is a "claims" array
If I use this command:
db.getCollection('requests').find({"claims.id": 1002})
It is not working in cosmosdb mongo api but working for local mongo service instance I have hosted.
my request object is as below
{
"_id" : NumberLong(1001),
"claims" : [ {
"type" : "broadband",
"id" : NumberLong(1002),
"createdOn" : NumberLong(1462799667905)
} ]
}

Not all of MongoDB's query syntax / capabilities are implemented. This appears to be such a case.
However, this slight workaround should work for you - I just tested it on my own CosmosDB (MongoDB API) collection:
db.getCollection('request').find({claims: { $elemMatch: { id:1002 }}}).pretty()
{
"_id" : 1001,
"claims" : [
{
"type" : "broadband",
"id" : 1002,
"createdOn" : NumberLong("1462799667905")
}
]
}
Note that you can also call db.request.find() without the need for a call to getCollection().

Related

Access document directly by ID

I'm used to working with firebase where I can access a document directly by fetching data from the db like so.
db.collection('collectionName/documentID').get();
I can't seem to find any documentation regarding doing something similar in mongodb. Do I have to use a find query to grab data from a mongodb or have I missed something? Thanks
I'm thinking
const collection = db.collection('collectionName');
collection.findOne({_id: ObjectId('documentID'); });
Since mongo consolse is an interactive javascript shell, One way would be to create a method similar to this:
function collectionNameGet(idToFind) {
return db.collection.find({_id: idToFind });
}
In the mongo shell you can directly get it as below:
db.st4.find({"_id" : "1234"})
Result set:
{ "_id" : "1234", "raw" : { "meas" : { "meas1" : { "data" : "blabla" }, "mesa2" : { "data" : "foo" } } } }
Or by default mongo id as:
db.st1.find({"_id" : ObjectId("5c578d57ce9ba4a066ca2fa4")})
{ "_id" : ObjectId("5c578d57ce9ba4a066ca2fa4"), "name" : "Just a name", "users" : [ "user1", "user2" ] }
For display the result in pretty format
db.st1.find({"_id" : ObjectId("5c578d57ce9ba4a066ca2fa4")}).pretty()
Result set:
{
"_id" : ObjectId("5c578d57ce9ba4a066ca2fa4"),
"name" : "Just a name",
"users" : [
"user1",
"user2"
]
}
Here st4 is my collection name in the database test, so once you are on mongo shell do the below steps before above query:
use test
db.st1.insert({"name" : "Just a name", "users" : [ "user1", "user2" ] })
and then you can query by default _id generated mongo, you can simply make a query to get the recently added documents in the collection st1 as below:
db.st1.find().sort({_id:-1}).limit(1)
Hope this will help you out to do some basic query on mongo shell

Can we use spring data repository to update embedded documents in mongodb

Can we use spring data repository to update embedded documents in mongodb
{
"_id" : 1000,
"user_id" : "001",
"events" : [
{
"handled" : 1,
"profile" : 10,
"data" : "....."
}
{
"handled" : 1,
"profile" : 10,
"data" : "....."
}
{
"handled" : 1,
"profile" : 20,
"data" : "....."
}
...
]
}
I want to update the handle to 10 where events.profile is 10.
I know how to do it using mongoTemplate but i need to know how to do it using mongoRepository. Thanks
As far as I understand you want to create a Repository with an update-Method? During my resarch in the spring-data reference i couldnt find any hint, that this is supported by spring data.
So in your case you could create a query like 'Collection findByEvents_Profile(Integer id)', iterate over the collection and persist it again by calling the repositories 'saveAll' method.

Data migration is not happening properly to CosmosDB using data Migration tool

I am exporting document using "Sudio 3T" and it generates below JSON.
{ "_id" : { "$oid" : "5aa8bf0077bbfe296c1727de" }, "_t" : "DiscreteProperty", "Name" : "AHRI_bCertified"}
Next I using migration tool import JSON to Azure CosmosDB. Result is, it uploads but not viewing the document because it doesn't _id field.
Note: I am not providing anything in "Id Field" of target screen(CosmosDB).
I followed your steps but did't reproduce your issue.Please refer to my details.
sample .json file:
[
{
"_id" : { "$oid" : "5aa8bf0077bbfe296c1727de" },
"_t" : "DiscreteProperty",
"Name" : "AHRI_bCertified"
}
]
target info:
import result:

Mongo db update query is not working in embedded documents in azure cosmos db

Embedded Update query works fine in mlab and atlas but not working in Cosmos DB:
My Collection structure:
{
"_id" : ObjectId("5982f3f97729be2cce108785"),
"password" : "$2y$10$F2P9ITmyKNebpoDaQ1ed4OxxMZSKmKFD9ipiU1klqio239c/nJcme",
"nin" : "123",
"login_status" : 1,
"updated_at" : ISODate("2017-05-16T09:09:03.000Z"),
"created_at" : ISODate("2017-05-16T06:08:47.000Z"),
"files" : [
{
"name" : "abc",
"updated_at" : ISODate("2017-05-16T06:08:48.000Z"),
"created_at" : ISODate("2017-05-16T06:08:48.000Z"),
"_id" : ObjectId("5982f3f97729be2cce108784")
}
],
"name" : "demo",
"email" : "email#gmail.com",
"phone" : "1231234",
}
My query is:
db.rail_zones.update(
{'_id': ObjectId("5982f3f97729be2cce108785"),
'files._id' : ObjectId("5982f3f97729be2cce108784")},
{ $set: {'files.$.name' : "Changed"}})
I get this response:
"acknowledged" : true,
"matchedCount" : 0.0,
"modifiedCount" : 0.0
According to your description, I tested this issue on my side and found the Array Update could not work as expected. I assumed that the Array Update feature has not been implemented in the MongoDB Compatibility layer of Azure CosmosDB. Moreover, I found a feedback Positional array update via '$' query support talking about the similar issue.

$elemmatch not working in MongoDB

I am trying to filter mongo data by using the below query in mongodb version 2.6.1 but getting error.
MongoDB version 2.4.6 (Working):
> db.BC_1839.find({data: {$elemMatch:{$where : "this.First_name.toLowerCase().indexOf('kim') ==0"}}});
output:
{
"_id" : ObjectId("53719a9d5b9e5c8c110001b9"),
"data" : [
{
"First_name" : "Kimberely",
"Last_name" : "Weyman",
"Company_name" : "Scientific Agrcltl Svc Inc",
"Address" : "7721 Harrison St",
"City" : "Kingsway West",
"State" : "NS",
"Post" : "2208",
"Phone1" : "02-7091-8948",
"Phone2" : "0441-151-810",
"Email" : "kweyman#weyman.com.au",
"Web" : "http://www.scientificagrcltlsvcinc.com.au",
"active" : "true"
}
],
"history" : [
{
"timestamp" : "2014-05-13 06:07:55",
"event": "creation",
"createdby" : "Srikesh Infotech",
"creation_data" : [
{
"crm_base_contact_id" : "1839",
"crm_imported_files_id" : "1464"
}
]
},
{
"timestamp" : "2014-05-13 06:09:05",
"event" : "Task",
"createdby" : "Srikesh Infotech",
"Task_data" : [
{
"Campaign ID" : "193",
"Campagin Name" : "Test Campa1"
}
]
}
],
"ref" : [
{ "crm_base_contact_id" : "1839", "crm_imported_files_id" : "1464" }
]
}
MongoDB version 2.6.1(Not Working):
> db.BC_1839.find({data: {$elemMatch:{$where : "this.First_name.toLowerCase().indexOf('kim') ==0"}}});
output:
error: {
"$err" : "Can't canonicalize query: BadValue $elemMatch cannot contain $
where expression",
"code" : 17287
}
Same query executes in mongodb version 2.4.6 but not in mongodb version 2.6.1 Why???
It shouldn't have worked in earlier versions at all, as at the very least you have modified the scoping of this to now refer to "data" as a top level element. In short, this is no longer allowed and you really should not be using JavaScript methods unless you absolutely have to. Even then, there is probably still a better way in most cases.
But in fact this is an un-necessary use of JavaScript matching as it is not required when there are other operators existing that will do this.
You should be using a $regex form instead:
db.docs.find({ "data.First_name": /^kim/i })
Or anywhere within the field, remove the caret ^:
db.docs.find({ "data.First_name": /kim/i })
Which is pretty much as inefficient as JavaScript execution but not as much as there is not the overhead of processing through that interpreter engine. And of course it works everywhere.
Also think about what a query relying on JavaScript to resolve is actually doing:
Invokes a JavaScript interpreter instance
Converts BSON document types per document to JavaScript types
Evaluates JavaScript code in the interpreter per document
Casts JavaScript true|false back as a result per document
Considering that $regex ( but with a case insensitive match which is not optimal ) is doing the same operations but using the "pcre" C library natively without conversion and recasting per document, then it is clearly the sane choice of the two.