I have a Favorite class that has a pointer to a class called Location as one of its location fields. I want to query the Favorite class and retrieve all objects that point to a certain Location object. In javascript I just create a Location object, set the id to the objectId of the location I am interested in and query for Favorite where location is equal to that object.
But in android there is no way I can set the objectId. Also setting the query just to the string of the objctId is not working.
// Something I would have expected existed
//ParseObject location = new ParseObject("Location");
//location.setObjectId(locationId);
final ParseQuery<ParseObject> query = ParseQuery.getQuery("Favorite");
// if I had the location object, but I only have the locationId
//query.whereEqualTo("location", location );
query.whereEqualTo("location", locationId);
query.findInBackground(...
query.whereEqualTo("location",Parse.createWithoutData(reference_classname,ObjectId));
Related
So basically:
const screamDocument = db.doc(`/screams/${req.params.screamId}`)
This line of code works because req.params.screamId is document ID. I just want to know how to write this line but getting document path with FIELD VALUE and not document ID. For example I want a document that inside collection scream but exactly one document which has field value "age" equal to 120. I know how I wqould do that the long way, with querysnapshot and doc.data bla bla but how to write it short and in one line like above but with field value?
like something like this:
const postDocument2 = db.collection("posts").where("id", "==", id);
but this lacks .doc and doesnt return to me what i want how to add .doc to that line?
Ok so I solved it myself, the solution is this: document path ONLY works if you include document ID and not a field called id, so for example this like
const screamDocument = db.doc(`/screams/${req.params.screamId}`)
is a document path because screamId is document ID of one of documents inside collection screams. If I want to have this document path with some field value, I must store document ID in a sepparate variable and then add it like this:
const screamDocument = db.doc(`/screams/${documentId}`)
now how to get documentId i mean id of a document thats easy to find snapshot.foreach(doc)
and u can get doc.id its in documentation.
I've a requirement where I need to re-use my sequence to write register present in different register map, the register name and structure(bit width, field endiness etc) differ in each map but the field name is same. Is there a way, where I can use the field name(string) to get register name or hierarchy in the register map.
Here's some code that gets a register name given a field name:
uvm_field field;
uvm_reg parent_reg;
string parent_reg_name;
field = m_env.m_serial_regmodel.get_field_by_name("TX_DMA_RUN");
parent_reg = field.get_parent;
parent_reg_name = parent_reg.get_name;
// etc
I am trying to delete Posts from a comments section in my web app. Mongodb passes an objectId but I am unable to get it from my angular 2 front end. By default ObjectId is passed as _id so on my front end I call Post._id in my delete function and it passes through all this info
what I actually want is the unique ObjectId given by the database itself that look like this
How can I get this value on the front end of my application? I have all the code to my project on github located here with both my UI and API backend. Thanks for any help!
relative files from repo
UI/src/app/components - PostData.Service.ts
UI/src/app/components -postRand.component.ts
UI/src/app/components/models - Post.ts
API/src/controllers - PostAPIController.cs
API/src/models - Post.cs
API/src/models - DataAccess.cs
API Running
I believe I need to make this fix in the api layer because it is passing my _id as an object and not a string... This is just a guess of mine and I am not sure how to do this.
I found out how to solve my issue....
I had to go back into my model and convert my ObjectId to a string by parsing it out
[BsonRepresentation(BsonType.ObjectId)]
public string Id
{
get { return Convert.ToString(_id); }
set { _id = MongoDB.Bson.ObjectId.Parse(value); }
}
I then added my new Id field to all of my http calls changing out where I was calling by ObjectId, to now call upon my new string value Id
Now when I run my Api get the actual Id of my object
Lastly I added my newly generated Id field to my front end, replacing the old objectId value in my delete functions with the new Id.
All code has been updated in my git project, see file referenced in question for relevant documents.
After review it looks like you are trying to delete the object by an object id string instead of an object id object.
In PostAPIController.cs
line 74
objds.Remove(post._id);
This is looking for an object with a string value to match on.
Pretty sure it should be like this
objds.Remove(ObjectId.Parse(post._id))
This will construct a object id to match on for the deletion
I try in Grails service save an object to mongodb:
Cover saveCover = new Cover()
saveCover.id = url
saveCover.url = url
saveCover.name = name
saveCover.sku = sku
saveCover.price = price
saveCover.save()
Cover domain looks like this:
class Cover {
String id
String name
String url
String sku
String price
}
So I want to have custom id based on url, but during save process I get error:
Could not commit Datastore transaction; nested exception is
org.grails.datastore.mapping.core.OptimisticLockingException: The
instance was updated by another user while you were editing
But if I didn`t use setters and just pass all values in constructor, the exception is gone. Why?
As reported in the documentation here:
Note that if you manually assign an identifier, then you will need to use the insert method instead of the save method, otherwise GORM can't work out whether you are trying to achieve an insert or an update
so you need to use insert method instead of save when id generator is assigned
cover.insert(failOnError: true)
if you do not define the mapping like this:
static mapping = {
id generator: 'assigned'
}
and will use insert method you'll get an auto-generated objectId:
"_id" : "5496e904e4b03b155725ebdb"
This exception occurs when you assign an id to a new model and try to save it because GORM thinks it should be doing an update.
Why this exception occurs
When I ran into this issue I was using 1.3.0 of the grails-mongo plugin. That uses 1.1.9 of the grails datastore core code. I noticed that the exception gets generated on line 847(ish) of NativeEntryEntityPersister. This code updates an existing domain object in the db.
Above that on line 790 is where isUpdate is created which is used to see if it's an update or not. isInsert is false as it is only true when an insert is forced and readObjectIdentifier will return the id that has been assigned to the object so isUpdate will end up evaluating as true.
Fixing the exception
Thanks to && !isInsert on line 791 if you force an insert the insert code will get called and sure enough the exception will go away. However when I did this the assigned id wasn't saved and instead a generated object id was used. I saw that the fix for this was on line 803 where it checks to see if the generator is set to "assigned".
To fix that you can add the following mapping.
class Cover {
String id
String name
String url
String sku
String price
static mapping = {
id generator: 'assigned'
}
}
A side effect of this is that you will always need to assign an id for new Cover domain objects.
Based on the data model here: Photographer<------->>Photo
When the user goes to add a Photo, the user also specifies what Photographer took said picture. When the user decides to save the Photo, there is the possibility that the Photographer doesn't exist yet. After executing a NSFetchRequest to get a list of all instances of Photographer, how do I check if the NSMutableArray(photographerArray) contains an object that has the same fullName attribute as what the user is currently adding?
The naive way would be to simply get all the fullName of the array and check if it's in it.
BOOL photographerExist = [[listOfPhotographer valueForKey:#"fullName"] containsObject:enteredFullName];
However, it would be way more easier to put that directly in your fetch request. Just add a predicate to it.
NSPredicate *fullNamePredicate = [NSPredicate predicateWithFormat:#"fullName = %#", enteredFullName];
fetchRequest.predicate = fullNamePredicate;
If the result of the fetch is empty, then the photographer doesn't exist yet.