Add new field To collection in MongoDB but the value based on value in another field - mongodb

How Can I add New Field To All Documents but Tha Value Based On value in Another Field?
Example :
{"_id":"1","city":"Amman"}
{"_id":"2","city":"Cairo"}
I Want to Add a new field to all documents (Capital) based on the city name if the city is Amman Capital will be true else false

I think what you trying to achieve is to add new field to each document based on the existing details in each of those document. Try this script with your collection details:
use YourDB;
db.YourCollection.find({}).forEach(function(doc){
if(doc.city =='whatever'){
doc.capital=true;
}else{
doc.capital=false;
}
db.YourCollection.save(doc);
});

Related

Firestore: Save document ID into the Field

I'm using Swift here and confused about how to save the document ID into itself field. Here is an example :
I have a collection named "menu"
here we can focus on the one and only document in the "menu" collection which saved the name field "ayam goreng". How do i save the document ID "
7D3fuw3fri6oj287SySW" to the field named "menu_id" inside the document?
As shown in the documentation on adding a document:
In some cases, it can be useful to create a document reference with an auto-generated ID, then use the reference later. For this use case, you can call doc():
let newCityRef = db.collection("cities").document()
// later...
newCityRef.setData([
// ...
])
In your case, you can then get the document ID from the new ref with:
newCityRef.documentId

MongoDB Complex Query with Java

We have following structure in MongoDB documents.
{
"id":"1111",
"keys":[
{
"name":"Country",
"value":"USA"
},
{
"name":"City",
"value":"LongIsland"
},
{
"name":"State",
"value":"NewYork"
}
]
}
Now using Springframework Query object, I figured out a way to pull the details using below syntax
query.addCriteria(
Criteria.where("keys.value").is(countryparam).
andOperator(
Criteria.where("keys.value").is(stateparam)
)
);
Two issue with this query model.
First issue is it is irrelevant if countryparam and stateparam are actually meant to match Country key name and State key name respectively. If just the values matches, the query returns the document. Means, if I have Country and City params, this just works if user passes Country and City values, even if they are swapped. So how can I exactly compare City to cityparam and State to Stateparam?
More complexity is if I have to extract the document basing on multiple key value pairs, I should be correspondingly able to match key name with respective value and query the document. How can I do this?
Thanks in advance!

Exclude field from full-text search

I need to do the full text search in the MongoDB (version 2.4). I use the following fragment of code.
DBObject textSearchCommand = new BasicDBObject();
textSearchCommand.put("text", "profile");
textSearchCommand.put("search", pattern);
textSearchCommand.put("limit", searchLimit);
textSearchCommand.put("filter",new BasicDBObject("personInfo", new BasicDBObject("$ne",null)));
CommandResult commandResult = mongoTemplate.executeCommand(textSearchCommand);
BasicDBList results = (BasicDBList) commandResult.get("results");
It works well but I want to exclude one field (person picture data) from the text search.
Note: I don't want to exclude this field from the result. I want that MongoDB does not search in this field.
Which fields to search in is determined when you create the text index. When you only want the text index to apply to selected fields, you need to provide these fields at creation like this for example:
db.articles.createIndex(
{
title: "text",
synopsis: "text",
content: "text",
tags: "text"
}
)
When this is not an option for some reason (like when you don't know all possible field names which might be relevant for text search), an (admittedly dirty) workaround could be to store the non-searchable content in a different data-type than a string, for example as binary data.

sharepoint 2010 get value of choice field through api call

I have a sharepoint list ImageList, i have a column which stores its type
The column name is ImageType
the choices are "profile pic","thumbnail" etc
i want to fetch these choice values for this field
i tried accessing it using
http://myintranet:2010/sites/ImagesGallery/_vti_bin/listdata.svc/ImageList?$expand=ImageType
but it is not containing the choice values in it!
How can i get them?
The query:
http://myintranet:2010/sites/ImagesGallery/_vti_bin/listdata.svc/ImageList?$expand=ImageType
returns list items values for List resource only.
In order to retrieve field resource itself, the different endpoint have to be specified, in particular:
http://myintranet:2010/sites/ImagesGallery/_vti_bin/listdata.svc/ImageListImageType
It is assumed that list name is ImageList and field name is
ImageType
Example
$.getJSON(endpointUrl)
.done(function(data)
{
//print field choice options
data.d.results.forEach(function(item){
console.log(item.Value);
});
})
.fail(function(error){
console.log(JSON.stringify(error));
});

Multi level MongoDB object querying by key

If you only know the key name (say "nickname"), but not the exact path to that key in the object.
e.g. nickname may be at the first level like:
{"nickname":"Howie"}
or at the second level:
{"user":{"nickname":"Howie"}}
Is it possible to query for nickname equal "Howie" that would return both documents?
Unfortunately there is no wild card that allows you to search for a field at any level in the db. If the position is not relevant and you can modify the document structure you have 2 choices here. You can store your document as
{ fieldname:"nickname", value : "Howie" }
{ fieldname:"user/nickname", value: "Howie" }
You can then query using
db.so.find({fieldname:/nickname/, value:"Howie"})
Alternatively you can store as
db.so.insert({value:"Howie", fieldpath:["nickname"]})
db.so.insert({value:"Howie", fieldpath:["user", "nickname"]})
The advantage with the second approach is that you can now index {fieldpath:1, value:1} and a query on it such as
db.so.find({fieldpath:"nickname", value:"Howie"})
will be an indexed query.