I have a document like below in mongo database and I have corresponding Model class Flight.java with all possible fields that are come in any time as update ?
{
"flight": {
"event": "Leg",
"version": "2",
"key": {
"fltNum": "1111",
"fltOrgDate": "2021-01-12",
"depSta": "BBB",
"dupDepStaNum": "0"
},
"leg": {
"stations": {
"arr": "VVV",
"dupArrStaNum": "0"
},
"times": {
"STD": "2021-01-12T20:30:00",
"STA": "2021-01-12T23:21:00",
"LTD": "2021-01-12T20:30:00",
"LTA": "2021-01-12T23:05:00",
"PTA": "2021-01-12T23:05:00"
},
"status": {
"leg": " ",
"dep": "S",
"arr": "S"
}
}
}
}
So I will get updates for this document continuously with existing fields or some new fields also. I am not sure which filed will get update.
So How can I update document using java MongoDB or Spring Data MongoDB?
Well, you have pojo which has all possible fields.
Every update requires three parts.
Find command to identify the doc to be updated
Doc which specifies what will be updated
Options specifies upsert, multi
You can iterate each fields in pojo and check for not null fields then add it to updateDoc
example update in java
Related
I need to get all documents whose e.g. "_id" field equal to another document field, e.g. "appId"
{
"_id": "xxxx-xxxx-xxxx-xxxx",
"_rev": "xxxx-xxxx-xxxx-xxxx",
"header": {
"appId": "xxxx-xxxx-xxxx-xxxx"
So what would be the query?
"selector": {
"_id": {
"$eq": header.appId
}
},
You can't do "sub queries" with Mango.
From what I see, you're trying to get all the documents listed by appId.
This could be done by using a view.
Your map function would be the following:
if(doc.header && doc.header.appId){
emit(doc.doc.header.appId,{_id: doc.header.appId});
}
The result would be a list of documents mapped by doc.header.appId.
If you query the view with ?include_docs=true, the documents would be joined to the response since we're doing a ManyToJoin join.
Lets say my test data is
db.multiArr.insert({"ID" : "fruit1","Keys" : "apple"})
db.multiArr.insert({"ID" : "fruit2","Keys" : "carrot"})
db.multiArr.find({'ID': {$in: ['fruit1', 'fruit2']}})
if i want to update or insert a ID i can do it using
db.multiArr.update(
{'ID': "fruit12"},
{
'ID': "fruit12"
"$push": {
"Keys": "tomato"
}
},
upsert=True
)
i want to update or insert multiple records, i know the below query inserts only 1 row
db.multiArr.update(
{"ID": {"$in: ["fruit123", "fruit1234"]}},
{"ID": "---", "Keys": "tomato"},
upsert=true
)
is there a way to update/insert multiple records?
Your thinking on this is the wrong way around. Instead you take the "array" and turn that into operations. Instead of sending multiple "requests" you send multiple "operations" in "one request". With Bulk Operations using .bulkWrite()
var newKeys = ["fruit123", "fruit1234" ];
db.multiArr.bulkWrite(
newkeys.map( key => {
return {
"updateOne": {
"filter": { "ID": key },
"update": { "$set": { "Keys": "tomato" } },
"upsert": true
}
}
})
)
Here using a regular JavaScript .map() to transform the array content of newKeys into an array of "bulk write operations" statements to send to the server. In other languages the basic concept remains the same.
Over the wire, the send and response with the server is done in "one request", whilst the "package" sent for execution contains multiple actions.
If needed the return of this method in all API's is a BulkWriteResult which contains details of matched documents, updates and upserts as appropriate.
I am new to NoSQL and morphia. I am using Morphia to query MongoDB.
I have a sample collection as below:
[
{
"serviceId": "id1",
"serviceName": "ding",
"serviceVersion": "1.0",
"files": [
{
"fileName": "b.html",
"fileContents": "contentsA"
},
{
"fileName": "b.html",
"fileContents": "contentsB"
}
]
},
{
"serviceId": "id2",
"serviceName": "ding",
"serviceVersion": "2.0",
"files": [
{
"fileName": "b.html",
"fileContents": "contentsA"
},
{
"fileName": "b.html",
"fileContents": "contentsB"
}
]
}
]
I would like to fetch an element in "files" List , given service name, service version and filename., using Morphia.
I was able to get what I want using the query below:
db.ApiDoc.find({ serviceName: "ding", serviceVersion: "2.0"}, { files: { $elemMatch: { fileName: "b.html" } } }).sort({ "_id": 1}).skip(0).limit(30);
What I tried so far :
I tried using "elemmatch" api that morphia has, but no luck.
query = ...createQuery(
Result.class);
query.and(query.criteria("serviceName").equal("ding"),
query.criteria("serviceVersion").equal(
"2.0"));
query.filter("files elem",BasicDBObjectBuilder.start("fileName", "a.html").get());
I seem to get the entire Result collection with all the files. I would like to get only the matched files(by filename).
can some one help me how I can get this to work?
Thanks
rajesh
I don't believe it's possible to get just the matching sub element. You can request just to have the 'files' array returned but all elements will be included in the result set and you will have to refilter in your code.
The other option is to make Files a collection of its own with a serviceId field and then you'll have more power to load only certain files.
It's possible to do that.
the filter doesn't really work like projection.
try this :
datastore.createQuery(Result.class)
.field("serviceName").equal("dong")
.field("serviceVersion").equal("2.0")
.field("files.filename").equal("a.html")
.project("files.$.filename", true);
To clarify: I have a document with a subdoc. I create a new document with the same data of the other one and it gets a new id. However, when I copy the subdoc array they do not get a new id.
Are subdocs id local to the parent doc? I.e. would the following be a problem?
[
{
"__v": 1,
"_id": "5214af03a9f53efa61000004",
"name": "Foo",
"subdocs": [
{
"thing": "value",
"_id": "5214af03a9f53efa61000006"
}
]
},
{
"__v": 0,
"name": "Foo",
"_id": "5214af03a9f53efa61000014",
"subdocs": [
{
"thing": "value",
"_id": "5214af03a9f53efa61000006"
}
]
}
]
There is a unique index on the _id field of documents stored directly in a collection, but not for embedded documents, nor is there any requirement that embedded documents have an _id field at all. The two documents you have provided are both valid to be stored in MongoDB in the same database (I'm interpreting your example as an array of two documents that are both stored directly in a collection together).
I have a collection with nested documents in it. Each document also has an _id field.
Here's an example of a documents structure
{
"_id": ObjectId("top_level_doc"),
"title": "Cadernos",
"parent": "4fd55bbc5d1709793b000008",
"criterias": {
"0": {
"_id": ObjectId("a_nested_doc"),
"value": "caderno",
"operator": "contains",
"field": "design0"
}
}
}
I want to be able to find the nested document just by searching it's _id
With this query
{
"criterias._id" : ObjectId("a_nested_doc")
}
It returns the parent document (i just want the one that's nested).
Ideally I would do this
{
"_id" : ObjectId("a_nested_doc")
}
And it would return the document with that id (either its nested or not).
Ps. I edited the "_id" values for the sake of simplicity just for this example.
You may have to live with selecting criterias._id (without writing a wrapper around the query, at least), but you can select the document itself by simply retrieving a subset of the fields.
http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields
// The simplest case converted to your use case
db.collection.find( { criterias._id : ObjectId("a_nested_doc") }, { criterias : 1 } );